Retention labels not deleting folder

raj chahal 36 Reputation points
2022-10-13T11:09:20.057+00:00

Hi, Created a retension policy to delete files after x amount of days. Applied it to a folder in SharePoint Online.
Files within the folder are deleted, but the folder remains in place with the retension label.

Can the folder be removed also?

250009-image.png

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,300 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Jinwei Li-MSFT 4,726 Reputation points Microsoft Vendor
    2022-10-14T09:26:07.983+00:00

    Hi @raj chahal ,

    As per my test, create retention policy has only two choices, one is Delete item automatically, another is Delete noting. You could only create policy to delete items instead of delete folder. This is by design.
    250453-image.png


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. raj chahal 36 Reputation points
    2022-10-14T15:06:42.89+00:00

    If its by design its not practical.. perhaps something is being missed?

    0 comments No comments

  3. Jinwei Li-MSFT 4,726 Reputation points Microsoft Vendor
    2022-10-18T05:33:38.14+00:00

    Hi @raj chahal ,

    If you want to delete a folder without files, I suggest you could use PNP-PowerShell to delete it. It can automatically identify an empty folder and delete it.

    Please try to use this code:

    #Parameters  
    $SiteURL = "https://crescent.sharepoint.com/sites/Marketing"  
    $DocumentLibraryName = "Documents"  
        
    #Connect to the Site  
    Connect-PnPOnline -URL $SiteURL -Credentials (Get-Credential)  
       
    #Get the web & folder  
    $Web = Get-PnPWeb  
    $List = Get-PnPList -Identity $DocumentLibraryName -Includes RootFolder  
       
    Function Delete-PnPEmptyFolder([Microsoft.SharePoint.Client.Folder]$Folder)  
    {  
        $FolderSiteRelativeURL = $Folder.ServerRelativeUrl.Substring($Web.ServerRelativeUrl.Length+1)  
        #Process all Sub-Folders  
        $SubFolders = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType Folder  
        Foreach($SubFolder in $SubFolders)  
        {  
            #Exclude "Forms" and Hidden folders  
            If(($SubFolder.Name -ne "Forms") -and (-Not($SubFolder.Name.StartsWith("_"))))  
            {  
                #Call the function recursively  
                Delete-PnPEmptyFolder -Folder $SubFolder  
            }  
        }  
        #Get all files & Reload Sub-folders from the given Folder  
        $Files = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType File  
        $SubFolders = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType Folder  
       
        If ($Files.Count -eq 0 -and $SubFolders.Count -eq 0)  
        {  
            #Delete the folder  
            $ParentFolder = Get-PnPProperty -ClientObject $Folder -Property ParentFolder  
            $ParentFolderURL = $ParentFolder.ServerRelativeUrl.Substring($Web.ServerRelativeUrl.Length+1)      
            Remove-PnPFolder -Name $Folder.Name -Folder $ParentFolderURL -Force -Recycle  
            Write-Host -f Green ("Deleted Folder: '{0}' at '{1}'" -f $Folder.Name, $Folder.ServerRelativeURL)  
        }  
       
    }  
    #Call the Function to Delete empty Folders  
    Delete-PnPEmptyFolder $List.RootFolder  
    

    251471-image.png

    Output:
    251481-image.png

    251423-image.png


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    0 comments No comments

  4. raj chahal 36 Reputation points
    2022-10-18T09:54:07.867+00:00

    @Jinwei Li-MSFT

    It can automatically identify an empty folder and delete it

    Is the code an event receiver, as I thought event receivers are not possible with SharePoint Online or must the code be run manually on a schedule?
    I expect, users to create folders as and when required, add retension label, then files and folders get deleted when retension period reached. If I have to manually run a script to remove empty folders becuse the retension does not, it's not a good solution.
    In the solution you provided, I may as well not add labels and delete folderes which contain the files. Correct me if I am wrong!