web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / delete empty folders i...
Power Automate
Answered

delete empty folders in sharepoint document library

(0) ShareShare
ReportReport
Posted on by 41

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

Thanks

Categories:
I have the same question (0)
  • ChristianAbata Profile Picture
    8,953 Most Valuable Professional on at

    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/

  • Verified answer
    faustocapellanj Profile Picture
    1,662 on at

    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,

     

  • auyiucho Profile Picture
    41 on at

    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",
     
  • fcapel1023 Profile Picture
    4 on at

    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.

     

  • faustocapellanj Profile Picture
    1,662 on at

    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,

  • auyiucho Profile Picture
    41 on at

    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.

     

     

  • auyiucho Profile Picture
    41 on at

    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

  • faustocapellanj Profile Picture
    1,662 on at

    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. 

  • auyiucho Profile Picture
    41 on at

    this time it shows error 404

    delete4.png

  • faustocapellanj Profile Picture
    1,662 on at

    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

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the March Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
Haque Profile Picture

Haque 605

#2
Valantis Profile Picture

Valantis 340

#3
11manish Profile Picture

11manish 284

Last 30 days Overall leaderboard