Fix “You have to delete all the items in this folder before you can delete the folder” Error in SharePoint Online

Problem: When trying to delete a folder with sub-folders and files, I got an error message “You have to delete all the items in this folder before you can delete the folder” in SharePoint Online or OneDrive.

You have to delete all the items in this folder before you can delete the folder

You may also see the “Request was cancelled by event received. If attempting to delete a non-empty folder, it’s possible that it’s on hold” error at times.

Request was cancelled by event received. If attempting to delete a non-empty folder, it's possible that it's on hold

Root Cause:

Usually, we don’t need to empty the folder before deleting it. However, If the site is on the preservation-retention policy or eDiscovery hold, we may get this error message. When the site is on hold, documents can be deleted, but we can’t delete a folder with files and sub-folders directly. This is how Preservation hold works to prevent data loss.

Solution for “You have to delete all the items in this folder before you can delete the Folder” Error:

Either you disable the Retention policy or eDiscovery Hold for the site from Office 365 Security and Compliance center as Global Administrator (and allow some time to take effect!), or as the error message says, delete all files and sub-folders inside the folder first! Deleting files from each folder can be time-consuming, So You can use “View in File Explorer” to delete a folder with files or PowerShell to make it easier.

SharePoint Online: Delete Folder with Files using PowerShell

This PowerShell script deletes all files and sub-folders of the given folder in SharePoint Online using the Remove-PnPListItem cmdlet.

#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$ListName ="Branding"
$FolderServerRelativeURL = "/sites/Marketing/Branding/2019"
 
Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive
      
    #Get All Items from Folder in Batch
    $ListItems = Get-PnPListItem -List $ListName -FolderServerRelativeUrl $FolderServerRelativeURL -PageSize 2000 | Sort-Object ID -Descending
  
    #Powershell to delete all files from a folder
    ForEach ($Item in $ListItems)
    {
        Remove-PnPListItem -List $ListName -Identity $Item.Id -Recycle -Force
        Write-host "Removed File:"$Item.FieldValues.FileRef
    }
}
Catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

More here: How to Empty a Folder in SharePoint Online using PowerShell?

If you have a larger library, The Get-PnPListItem cmdlet with FolderServerRelativeURL parameter may fail – even though you used “PageSize”. So, Here is the PowerShell script to delete everything from a given folder in SharePoint Online:

#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/mumbai"
$ListName ="Work"
$FolderServerRelativeURL = "/sites/mumbai/w/Arun Tiwari/New folder/*"

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -UseWebLogin
    $List = Get-PnPList $ListName
       
    #Get All Items from Folder in Batch
    $global:counter = 0;
    $ListItems = Get-PnPListItem -List $ListName -PageSize 2000 -Fields ID,FileDirRef,FileRef -ScriptBlock `
        { Param($items) $global:counter += $items.Count; Write-Progress -PercentComplete ($global:Counter / ($List.ItemCount) * 100) -Activity `
                "Getting Items from Folder '$FolderServerRelativeURL'" -Status "Getting Items $global:Counter of $($List.ItemCount)";}

    #Get List Items from the folder and Sort List Items based on Server Relative URL - Descending
    $ItemsFromFolder = $ListItems | Where {$_.FieldValues["FileDirRef"] -like $FolderServerRelativeURL } | Select-Object @{Name="ServerRelativeURL";Expression={$_.FieldValues.FileRef}}, ID | Sort-Object -Descending -Property ServerRelativeURL
    Write-host "Total Number of Items Found in the Folder:"$ItemsFromFolder.count
         
    #Delete List Items in Batch
    $Batch = New-PnPBatch
    ForEach ($Item in $ItemsFromFolder)
    {
        Remove-PnPListItem -List $ListName -Identity $Item.ID -Recycle -Batch $Batch
        Write-host "Item Queued for deletion:"$Item.ServerRelativeURL
    }
    Invoke-PnPBatch -Batch $Batch -Details

}
Catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

This error may happen when you have files checked out! Check them in all to fix: SharePoint Online: PowerShell to Bulk Check In All Documents

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

3 thoughts on “Fix “You have to delete all the items in this folder before you can delete the folder” Error in SharePoint Online

  • Hi I’m still having the same issue so could you help me?. it’s still saying “You have to delete all the items in this folder before you can delete the folder”

    Reply
  • Hi Salaudeen Rajack,

    when running script i get the following error after sometime:

    ErrorMessage ResponseCode Request
    ———— ———— ——-
    The operation has timed out. 500 https://domain.sharep…

    Can you please help

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *