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 / Removal of large numbe...
Power Automate
Unanswered

Removal of large number empty folders from a Sharepoint Document library

(1) ShareShare
ReportReport
Posted on by 986

Hello everyone,
Few days back I got a requirement wherein I had to delete all the empty folders present inside a Document library in a SharePoint list. For which I created a cloud based flow (manual triggered) to achieve the required. The flow looked as follows:

Sidhant_02_0-1720177531003.png

Sidhant_02_1-1720177584672.png

So this worked fine when I tested it in my quality setup.

But the actual issue was due to some issue (there was another application through which they were creating folders based on projects and there were some issues in the folder logic i.e. it got stuck in infinite loop which led to creation of large number of empty folders like 1 crore/10 million) and when they tried to execute this flow it got failed.
It gave 502 Error (Bad gateway error). So is it because of the high number of records?.

If so in such case (high number of records i.e. empty folders to be deleted is there any way to do it, if so could you please help me with it. As I have never dealt with such huge number of records till now.

Regards,
Sidhant

Categories:
I have the same question (0)
  • Damin Profile Picture
    63 on at

    Hi, for me I have two ways to achieve what you are looking for : 
    First, whether you create a reccuring flow that calls a child one which will delete only top-500 folder each time it gets called, ok.
    Second is to create a flow that call a runbook to executes a powershell script on azure automation, and this script deletes all the folder you'd like.
    Hope this could help you.

  • Sidhant_02 Profile Picture
    986 on at

    Hi @Damin ,
    Will it be possible to demonstrate with an example the two methods that you just mentioned.
    As I just got to know there are around 10,500 empty folders in the SharePoint document library

    Regards,
    Sidhant.

  • Sidhant_02 Profile Picture
    986 on at

    Hi @Damin ,
    I have created a new flow that is able to retrieve records and deletes 100 empty folders at a time. So I did try to increase the limit (threshold limit) but the loop only ran 100 times:

    Sidhant_02_0-1720698023645.pngSidhant_02_1-1720698084247.png


    Any updates so that I can delete all empty folders in a single run or if not possible then what is the other way.

    Regards,
    Sidhant.

  • Damin Profile Picture
    63 on at

    Hi, First of all, you'll need the sharepoint site id, and here is how to get it https://companyname.sharepoint.com/sites/sitename/_api/site/id

    Damin_3-1720812765028.png

     

    Then you need to register an application and give it Graph API application permissions, to do that you need to go to https://entra.microsoft.com/  
    once the app registered and the graph api permission (Files.ReadWrite.All) given to it:

    Damin_5-1720812937517.png

    Note Client Id, Tenant Id, and create Secret Key for the application.Damin_4-1720812783224.png

    Now the flow

    Damin_6-1720813126257.png

    To Get All Folders in Document Library (of course you can choose the library you want, root==Document Library)
    https://graph.microsoft.com/v1.0//sites/{siteId}/drive/root/children

    Damin_9-1720813313706.png

    Then filtering the http response body : 

    Damin_10-1720813457202.pngDamin_11-1720813486212.png

    Then deleting each item of the filtered array.

    Damin_12-1720813619893.png

     

    I hope this could help.

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
David_MA Profile Picture

David_MA 298 Super User 2026 Season 2

#2
11manish Profile Picture

11manish 150 Super User 2026 Season 2

#3
trice602 Profile Picture

trice602 147 Super User 2026 Season 2

Last 30 days Overall leaderboard