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 / Using Power Automate, ...
Power Automate
Suggested Answer

Using Power Automate, get and delete all empty SharePoint folders

(2) ShareShare
ReportReport
Posted on by 44
I have tried to mimic 4 different methods using Co-Pilot and YouTube videos.  And nothing works.
I do know that Power Automate and SharePoint change but this should NOT be that difficult for a simple task.
 
My site is called DND, my Library is called Sigma

I want the flow to find ALL empty folders, including subfolders, and delete them.  I have to ensure that any files on the root remain untouched.

I have tried the multiple variable method, the Do Until loop, the Send HTTP method.

Does anyone have something simple, without massive JSON knowledge that I can use to accomplish this.
 
I would like this to run with a manual trigger.

Appreciate everyone's assistance. 
I have the same question (0)
  • Suggested answer
    Pstork1 Profile Picture
    69,556 Most Valuable Professional on at
    Are you trying to delete the folders in a loop running on the array of folders?  That's the most common problem. You can't delete something from an array being used in loop because it will change the loop itself.  You need to loop through all the folders and find the ones that have nothing in them.  As you find each folder add it to a separate array variable of folder paths.  Then do a loop on that array to delete each folder.

    ----------------------------------------------------------------------------------
    If this Post helped you, please click "Does this answer your question" and give it a like to help others in the community find the answer too!

    Paul Papanek Stork, MVP
    Blog: https://www.dontpapanic.com/blog
     
  • David_MA Profile Picture
    14,956 Super User 2026 Season 1 on at
    I have not fully tested this, so if you try this I would try it on something you don't care about first. Of course, if it deletes content accidentally, you should be able to restore it from the recycle bin. I have only tested this up to the point of determining if the folder needs to be deleted or not. I may have a need tor something like this for a project I have, but it will require testing.
     
    But, maybe someone else will see this and can confirm this is the correct approach.
    The first get files properties Folders action uses the following filter query: FSObjType eq 1. This limits the action to only folders.
     
    This is how I configured the delete file action:
    It uses the Identifier value from the first get files action. From what I have read, this should delete the folder only if it is empty. Otherwise, it will fail. But that is why we have the second get files action to list the files in the folder and check how many there are based on the condition. The expression in the condition is 
    length(outputs('Get_files_(properties_only)')?['body/value'])
     
    The second get files action doesn't have a filter, so it will count folders in the total. So a folder that has folders but not files should not get deleted. Therefore, my assumption is that if this works, you may need to run the flow several times as it needs to get to the deepest folder, and if there are no files or subfolders in it, it will delete the folder.
  • Suggested answer
    Pstork1 Profile Picture
    69,556 Most Valuable Professional on at
    In my experience trying to Delete files or folders inside a loop running on the list of those files and folders does not work because you are changing the array the loop is running on.  I've also not had good luck deleting folders using the file Identifier.  So here's a similar flow that stores the List Item ID of the folder in an array and then deletes them using that array variable. This is tested and it does work.  The one issue is that Power Automate doesn't do recursion so it won't delete a folder that has only an empty folder in it.  It needs to be run a second time for that.

    ----------------------------------------------------------------------------------
    If this Post helped you, please click "Does this answer your question" and give it a like to help others in the community find the answer too!

    Paul Papanek Stork, MVP
    Blog: https://www.dontpapanic.com/blog
     
  • David_MA Profile Picture
    14,956 Super User 2026 Season 1 on at
    @Pstork1 has tested his method, @CU08031624-0 I would try that first. However, I did forget to mention one thing, which would apply to either method. The get files (properties only) action by default will return only the first 100 items. If you have a large document library, you will need to enable paging and set a limit for the number of items to return. At least in my version, the second Get files action can use the default, as the folder is not going to be deleted if there is one or more items. Therefore, there is no need to check if it contains more than 100 items.
  • Pstork1 Profile Picture
    69,556 Most Valuable Professional on at
    I should have showed in the screenshot that I'm using an OData filter query on the Get Files (Properties Only) to only return folders.  Unless you have a lot of folders that should take care of the Top Count issue.

    ----------------------------------------------------------------------------------
    If this Post helped you, please click "Does this answer your question" and give it a like to help others in the community find the answer too!

    Paul Papanek Stork, MVP
    Blog: https://www.dontpapanic.com/blog
     

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 Launch!

Jump in, show your community spirit, and win prizes!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
Valantis Profile Picture

Valantis 377

#2
11manish Profile Picture

11manish 279

#3
David_MA Profile Picture

David_MA 234 Super User 2026 Season 1

Last 30 days Overall leaderboard