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

Notifications

Announcements

Community site session details

Community site session details

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

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,951 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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 522 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 364 Moderator

#3
abm abm Profile Picture

abm abm 243 Most Valuable Professional

Last 30 days Overall leaderboard