Error when deleting SharePoint Online folder or file


You may encounter an error while deleting a folder in SharePoint Online - You have to delete all the items in this folder before you can delete the folder.

This usually occurs when the folder has sub-folders or files within and the SharePoint site has the preservation 'On-Hold' policy enabled for DLP (data loss prevention). When the site is on hold, files can be deleted directly but folders cannot be deleted unless all sub items are deleted.

Delete SharePoint folder
Delete SharePoint folder

⛏️ SOLUTION

The error message hints a solution - delete all files/folders within the folder and then delete the main folder.
Here are few ways you can try, whichever is convenient.
  1. Disable the on-hold for the site from Microsoft 365 Security and Compliance center (required Global Admin permissions). You can re-enable after deleting the folder.
  2. Delete all the files and sub-folders manually.
  3. Use PowerShell to write a script, loop through all sub items, and delete them.
  4. Use "View in File Explorer" option to manually delete files, this will give you a desktop like feeling.
  5. Ensure you have no checked out files in the folder.

You may come also come across a strange behavior, getting error 'You have to delete all the items in this folder before you can delete the folder' while deleting a SharePoint file.

This may happen when the library is corrupted (say due to a migration) and files are treated as folders.
Check if you have a default Content Type associated with the document library, if not, then add a default Content Type "Document" and set it for the problematic file from the document properties screen.

The same steps apply to OneDrive for Business, if you face same issue.