top of page

Safely Delete SharePoint Folders & Document Sets Using Power Automate

How to delete SharePoint folders in Power Automate

Today, I experienced a potentially disastrous outcome when trying to delete a SharePoint document set using Power Automate that resulted in me deleting an entire Document Library! In this article I will demonstrate how to delete a SharePoint folder or Document set using Power Automate.


Luckily, this happened in a development tenant where there was no loss of real world data. However, it created the task of investigating how this happened and how I could prevent it from happening in the future.


Before we begin, I want to give a huge thanks to William Johnson for assisting with this resolution.


How I deleted an entire SharePoint Document Library!

So, here is what went wrong. I decided to call a Send an HTTP request to SharePoint action where I could point to the Full Path of my Folder or Document Set (from a previous action where I was retrieving the object).

Avoid seperation between SharePoint list or library and the file, folder or item

What went wrong?

After investigation, it proved that the Full Path (being dynamic content) could potentially be blank or contain characters that the URI would be unable to process, therefore the string itself could be cut off at the end of the Document Set or Folder location (in this case, 'Repository').


How to Safely Delete SharePoint Folders & Document Sets

First begin with a Get Files (properties only) action, to retrieve a Document Set or Folder. Do this by supplying a Site Address, Library Name and Filter Query to pull a specific folder.


Next, we first need to find the Server Relative URL for the specific folder we are deleting. We can do this by expanding the folder properties by implementing another Send an HTTP request to SharePoint using the GET Method. Use the following URI:


_api/web/lists/getByTitle('ENTER_YOUR_SHAREPOINT_LIBRARY')/items(@{items('Apply_to_each')?['ID']})?$expand=Folder,File

Note: Replace the syntax with your SharePoint Library name. The Apply to each passes in the value from the Get files (properties only) action.


I have also renamed this action to Get Folder Properties.

Get the SharePoint folder properties

Finally we can add another Send an HTTP request to SharePoint action within the Apply to each to perform the DELETE Method.


_api/web/GetFolderByServerRelativeUrl('@{body('Get_Folder_Properties')?['d/Folder/ServerRelativeUrl']}')

This will essentially parse the results that come back from the body of the Get Folder Properties action and allow you to retrieve the ServerRelativeUrl.

Delete SharePoint folder using ServerRelativeURL

Explanation

By combining the Site Library URL and the Full Path in to a single Server Relative URL ensures the process is more resilient if the Document Set or Folder returns invalid characters or throws an error. If this happens the entire URI will fail and not be run.


What does this mean? You should not run into the issue stated at the beginning of this article.


I'll leave the error handling to you, but this approach will give you a solid foundation on how to handle SharePoint folder and document set deletion securely using Power Automate.

bottom of page