In this post, I will show you how to delete empty folders in SharePoint Document Libraries using Power Automate. This flow is manually triggered, but it can be created using any other trigger, such as a schedule or an automated trigger from any connector.
Scan a SharePoint Document Library and delete all folders that don’t have content, such as nested folders or files.
Note: this flow uses actions from the SharePoint REST API. Don’t be afraid; you don’t need to be a developer to do this. The post will explain the actions being used 😀.
To create the flow, please follow the steps below:
- Click on Create on the left menu and then click on Instant Flow.
- Enter the Flow name, select the Manually trigger a flow trigger and click on Create.
- Once the flow is in Edit mode, click on + New step to add the first action.
- Search for SharePoint http and select the Send an HTTP request to SharePoint. I renamed this action to GetFolders.
- For the Site Address property, select the desired site and set the Method property to GET. Once that’s done, set the Uri property to _api/web/GetFolderByServerRelativeUrl(‘/sites/YourSiteName/YourDocumentLibraryName’)/Folders. Finally, set the Headers property as follows: set the Key to accept and the Value to application/json;odata=nometadata.
- For the next action, add a Filter array (Data Operation). This action will be used to filter the output of the Send an HTTP request to SharePoint action above. First, set the * From value using this expression:
body('GetFolders')?['value']. Because there are 2 parameters this action will filter on, you need to edit the filter in advanced mode. To do this, click on Edit in advanced mode and set the filter to this expression:
@and(equals(item()?['ItemCount'], 0), not(equals(item()['Name'], 'Forms')).
The first part of the filter checks if the item count for each filter is equals to 0 and the second part checks if the folder names are not equal to Forms. Forms is an internal folder that comes out of the box with every document library and cannot be deleted.
- For the next action, add an Apply to each, which I renamed to ForEach Folder, and set the * Select an output from previous steps to the Body from the Filter array action. This action will loop through each folder found in the output of the Filter array action above.
- For the next and final action, add another Send an HTTP request to SharePoint inside the Apply to each action listed above. Please see step 4 above for instructions. I renamed this action to Delete Folders.
- For the Site Address property, select the desired site and set the Method property to DELETE. Once that’s done, set the Uri property to _api/web/GetFolderByServerRelativeUrl(‘/sites/YourSiteName/YourDocumentLibraryName/items(‘ForEach_Folder’)[‘Name’]‘). This part (
items('ForEach_Folder')['Name']) is an expression and must be entered from the Expression tab.
- Save the flow and run it. If you followed all the instructions as explained above and there are empty folders in the targeted document library, the flow will work as expected 😀.
I hope this blog post has been informative and if you have any questions, please to do not hesitate to reach out. Thank you for reading.