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


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 the 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 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 the same issue.

Have Questions? Post them here!
Try Out Code2care Dev Tools:


Android Java Linux Microsoft Google Python macOS Notepad++ Microsoft Teams CSS PHP SharePoint Html Linux C Programs Bootstrap jQuery Sublime Android Studio Facebook Eclipse WhatsApp News MySQL Json HowTos JavaScript FTP S3


Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and Software Developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include Learnings, Tutorials, Trouble-Shooting steps, Video Tutorials, Code Snippets, How Tos, Blogs, Articles, etc.
🎉 We are celebrating the 10th years of Code2care! Thank you for all your support! We hope we made a difference.
We strong support Gender Equality & Diversity.

We stand in solidarity with Ukraine - Make a donation to UNHCR -