How-To #4: Delete Empty Folders in SharePoint Document Libraries Using Power Automate

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.

Scenario:
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 😀.

Solution:
To create the flow, please follow the steps below:

  1. Click on Create on the left menu and then click on Instant Flow.

  2. Enter the Flow name, select the Manually trigger a flow trigger and click on Create.

  3. Once the flow is in Edit mode, click on + New step to add the first action.
  4. Search for SharePoint http and select the Send an HTTP request to SharePoint. I renamed this action to GetFolders.

  5. 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.

  6. 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.

  7. 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.

  8. 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.
  9. 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.

  10. 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.

Photo by Steve Johnson on Unsplash

6 thoughts on “How-To #4: Delete Empty Folders in SharePoint Document Libraries Using Power Automate

  1. When you use _api/lists/getbytitle(‘Documents’)/items?$filter=FSObjType%20eq%201&$select=Id,LinkFilename,Folder/itemCount&$expand=Folder, you get the folders AND the itemcount. with that you can decide if you want to delete or not. the FSObjtype shows only the folders, the Folder/Itemcount shows you the nr of files or folders.

    Like

      1. I get stuck on #6 – body(‘GetFolders’)?[‘value’] returns an expression (not dynamic content), and it says “body”, which is rejected when I try to save. I’d love to figure this out; we have so many empty folders!

        Like

  2. I keep getting an error on the filter array. Likely the same one as Caite Hoover Stevens did. In step number 6, in the From value field I used Dynamic content – Expression and typed in body(‘GetFolders’)?[‘value’]. Upon hitting ok, it added the pink function icon block with the expression in it, not the green Sharepoint icon Value one you have in your example. I then switched to advanced mode and added the next expression, @and(equals(item()?[‘ItemCount’], 0), not(equals(item()[‘Name’], ‘Forms’)) in the field below it.

    The error says “Fix invalid expressions(s) for the input parameters(s) of operation ‘Filter_array’.

    I’m doing this as a scheduled cloud flow on a monthly recurring basis, so I tried a second time using the manual flow as your example showed and still got the same error.

    Like

Leave a Reply to Rachel Campbell Cancel reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s