cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Helper I
Helper I

delete empty folders in sharepoint document library

how to delete all empty folders in a sharepoint document library?

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Solution Sage
Solution Sage

Re: delete empty folders in sharepoint document library

Hello @auyiucho 

As @ChristianAbata mentioned, you need to use the SharePoint HTTP request. This is how you would do it:

  1. For my example, I used a manual trigger.
  2. For the next action, add a Send an HTTP request to SharePoint, which in my example I renamed to GetFolders. Set your Site Address and for Method, use Get. In the Uri, enter the following: 
    _api/web/GetFolderByServerRelativeUrl('/sites/YourSiteName/YourDocumentLibraryName')/Folders. This will return all the folders inside the respective document library. For the Headers, set the Key to accept and its Value to application/json;odata=nometadata. Adding this to your Headers will reduce the amount of unnecessary data the HTTP request will send back.
    delete-1.png
  3. For the next action, add a Filter array. This will be used to retrieve the folders that have 0 ItemCount and to exclude an internal folder called Forms. In the * From field, enter the expression body('GetFolders')['Value']. which is the array returned in the from the HTTP request. Click on Edit in advanced mode and use the expression @and(equals(item()?['ItemCount'], 0), not(equals(item()['Name'], 'Forms'))).
    delete-2.png
  4. For the next action, add an Apply to each, which in my example I renamed to ForEach Folder. For the * Select an output from previous steps, select the Body from the Filter array action. Inside this action, add another Send an HTTP request to SharePoint, which in my example I also renamed to Delete Folders
  5. Set your Site Address and for Method, use Delete. In the Uri, enter the following: 
    _api/web/GetFolderByServerRelativeUrl('/sites/YourSiteName/YourDocumentLibraryName/ExpressionGoesHere'). For the ExpressionGoesHere part in the Uri, use the expression items('ForEach_Folder')['Name'].
    delete-3.png

Please try this approach and let us know.

Regards,

 


Fausto Capellan, Jr


Did I answer your question? Mark my post as a solution! Did my response help? Please give it a thumbs up!

Proud to be a Flownaut!


View solution in original post

16 REPLIES 16
Highlighted
Super User
Super User

Re: delete empty folders in sharepoint document library

hi @auyiucho  the only way I can see you can do this is using HTTP sharepoint request

 

https://blogs.msmvps.com/windsor/2018/06/15/calling-the-sharepoint-rest-api-in-a-microsoft-flow/



Did I answer your question? Please consider to Mark
my post as a solution! to guide others :winking_face:

Proud to be a Flownaut!


If you want you can follow me at www.christianabata.com Quieres contenido en español? Síguenos en Power Automate LA
Highlighted
Solution Sage
Solution Sage

Re: delete empty folders in sharepoint document library

Hello @auyiucho 

As @ChristianAbata mentioned, you need to use the SharePoint HTTP request. This is how you would do it:

  1. For my example, I used a manual trigger.
  2. For the next action, add a Send an HTTP request to SharePoint, which in my example I renamed to GetFolders. Set your Site Address and for Method, use Get. In the Uri, enter the following: 
    _api/web/GetFolderByServerRelativeUrl('/sites/YourSiteName/YourDocumentLibraryName')/Folders. This will return all the folders inside the respective document library. For the Headers, set the Key to accept and its Value to application/json;odata=nometadata. Adding this to your Headers will reduce the amount of unnecessary data the HTTP request will send back.
    delete-1.png
  3. For the next action, add a Filter array. This will be used to retrieve the folders that have 0 ItemCount and to exclude an internal folder called Forms. In the * From field, enter the expression body('GetFolders')['Value']. which is the array returned in the from the HTTP request. Click on Edit in advanced mode and use the expression @and(equals(item()?['ItemCount'], 0), not(equals(item()['Name'], 'Forms'))).
    delete-2.png
  4. For the next action, add an Apply to each, which in my example I renamed to ForEach Folder. For the * Select an output from previous steps, select the Body from the Filter array action. Inside this action, add another Send an HTTP request to SharePoint, which in my example I also renamed to Delete Folders
  5. Set your Site Address and for Method, use Delete. In the Uri, enter the following: 
    _api/web/GetFolderByServerRelativeUrl('/sites/YourSiteName/YourDocumentLibraryName/ExpressionGoesHere'). For the ExpressionGoesHere part in the Uri, use the expression items('ForEach_Folder')['Name'].
    delete-3.png

Please try this approach and let us know.

Regards,

 


Fausto Capellan, Jr


Did I answer your question? Mark my post as a solution! Did my response help? Please give it a thumbs up!

Proud to be a Flownaut!


View solution in original post

Highlighted
Helper I
Helper I

Re: delete empty folders in sharepoint document library

Thanks faustocapellanj.

 

My document library as below

delete2.png

So, I enter the site/library as below

delete1.png

But it returns error: 400

"message""{\"odata.error\":{\"code\":\"-2147024809, System.ArgumentException\",\"message\":{\"lang\":\"en-US\",\"value\":\"Server relative urls must start with SPWeb.ServerRelativeUrl\"}}}\r\nclientRequestId: eb1a8ad5-0f36-4a9a-8a56-f0e9f635e75d\r\nserviceRequestId: af375a9f-f0dd-0000-537e-631771881cae",
 
Highlighted
Regular Visitor

Re: delete empty folders in sharepoint document library

Hi @auyiucho 

The parameter in the GetFolderByServerRelativeUrl action needs to match the URL of the site you are sending the HTTP request to. From what I see in your screenshot, your site is structured as /teams/admin, not as the example I posted before. Craft your folder Uri as follows: /teams/admin/InconSystemTemp and try again.

 

Highlighted
Solution Sage
Solution Sage

Re: delete empty folders in sharepoint document library

To clarify something, the reply posted by @fcapel1023 was done by me, @faustocapellanj , but didn't realize I had done it from an old account I never use, which was logged in in my phone. Anyways, apologies if this causes any confusion.

See my actual reply below: 
The parameter in the GetFolderByServerRelativeUrl action needs to match the URL of the site you are sending the HTTP request to. From what I see in your screenshot, your site is structured as /teams/admin, not as the example I posted before. Craft your folder Uri as follows: /teams/admin/InconSystemTemp and try again.

Regards,


Fausto Capellan, Jr


Did I answer your question? Mark my post as a solution! Did my response help? Please give it a thumbs up!

Proud to be a Flownaut!


Highlighted
Helper I
Helper I

Re: delete empty folders in sharepoint document library

I double check the site name. it is "admin"

delete3.png

So, I type the Uri as below:

_api/web/GetFolderByServerRelativeUrl('/sites/admin/InconSystemTemp')/Folders

 

But the same error 400.

 

 

Highlighted
Helper I
Helper I

Re: delete empty folders in sharepoint document library

It has error details:

{"odata.error":{"code":"-2147024809, System.ArgumentException","message":{"lang":"en-US","value":"Server relative urls must start with SPWeb.ServerRelativeUrl"}}}
clientRequestId: 1d57bf9d-84ad-4ccb-8e9d-fe458217f1be
serviceRequestId: 703f5a9f-e0bd-0000-530b-eeaf785e43b9

Highlighted
Solution Sage
Solution Sage

Re: delete empty folders in sharepoint document library

Hi @auyiucho 

If you look at the Site Address you are using in your Send an HTTP request to SharePoint action, the site is as follows: https://inconhk.sharepoint.com/teams/admin, but in the Uri, you are using _api/web/GetFolderByServerRelativeUrl('/sites/Administration/InconSystemTemp')/Folders. The text highlighted in Red doesn't match the Url in the Site Address parameter.

Your Uri parameter must be _api/web/GetFolderByServerRelativeUrl('/teams/admin/InconSystemTemp')/Folders to match the Site Address. To be safe, open a browser window and enter this URL: 
https://inconhk.sharepoint.com/teams/admin/_api/web/GetFolderByServerRelativeUrl('/teams/admin/InconSystemTemp')/Folders and see what you get. 


Fausto Capellan, Jr


Did I answer your question? Mark my post as a solution! Did my response help? Please give it a thumbs up!

Proud to be a Flownaut!


Highlighted
Helper I
Helper I

Re: delete empty folders in sharepoint document library

this time it shows error 404

delete4.png

Highlighted
Solution Sage
Solution Sage

Re: delete empty folders in sharepoint document library

Hi @auyiucho 

The screenshot below shows what your Send an HTTP request to SharePoint should look like, especially the * Uri parameter. Please try that and let me know.

delete-4.png


Fausto Capellan, Jr


Did I answer your question? Mark my post as a solution! Did my response help? Please give it a thumbs up!

Proud to be a Flownaut!


Highlighted
Super User
Super User

Re: delete empty folders in sharepoint document library

@auyiucho  I see you are using get Method and the correct is delete, please change it and try

 

delete4.png



Did I answer your question? Please consider to Mark
my post as a solution! to guide others :winking_face:

Proud to be a Flownaut!


If you want you can follow me at www.christianabata.com Quieres contenido en español? Síguenos en Power Automate LA
Highlighted
Solution Sage
Solution Sage

Re: delete empty folders in sharepoint document library

Hello @ChristianAbata 

The one he seems to be having troubles with is the Get HTTP method, not the Delete since it's a different Uri.


Fausto Capellan, Jr


Did I answer your question? Mark my post as a solution! Did my response help? Please give it a thumbs up!

Proud to be a Flownaut!


Highlighted
Helper I
Helper I

Re: delete empty folders in sharepoint document library

Thanks faustocapellanj.

I correct the Uri. It works.

But the Filter array cannot pass.

delete5.png

Highlighted
Solution Sage
Solution Sage

Re: delete empty folders in sharepoint document library

Hi @auyiucho 

I'm glad you got that part working. Now for the Filter array, remove the expression and set it back to Basic mode. On the left side, do the expression item()['ItemCount'], set the logical operator to is equals to and then set the value of the right to 0. Let me know if the filter array works.


Fausto Capellan, Jr


Did I answer your question? Mark my post as a solution! Did my response help? Please give it a thumbs up!

Proud to be a Flownaut!


Highlighted
Helper I
Helper I

Re: delete empty folders in sharepoint document library

Thanks faustocapellanj so much.

 

It finally works.

 

Highlighted
Solution Sage
Solution Sage

Re: delete empty folders in sharepoint document library

You're welcome!

Fausto Capellan, Jr


Did I answer your question? Mark my post as a solution! Did my response help? Please give it a thumbs up!

Proud to be a Flownaut!


Helpful resources

Announcements
firstImage

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

firstImage

Join the new Power Virtual Agents Community!

We are excited to announce the launch of Power Virtual Agents Community. Check it out now!

firstImage

New & Improved Power Automate Community Cookbook

We've updated and improved the layout and uploading format of the Power Automate Cookbook!

thirdimage

Power Automate Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

Top Solution Authors
Top Kudoed Authors
Users online (9,061)