cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
overhead_press2
Frequent Visitor

Auto delete files in SharePoint subfolders based file name

I have a bunch of subfolders in SharePoint and I am wondering about how would I go about automatically deleting files within them if the folder name doesn't match the beginning part of the file name? For example in my screenshot, the sub folder name is "J0000-1002", so I would remove any file that doesn't start with "J0000-1002". The sub folders will have different names so this would need to be dynamic based on the sub folder name.

1.png

 

I am fairly new to Power Automate and was wondering if this is possible. I was thinking of using the trigger "When a file is created or modified" but not sure how to apply it to multiple sub folders. Any help would be appreciated, thanks! 

1 ACCEPTED SOLUTION

Accepted Solutions
NiloferA
Responsive Resident
Responsive Resident

Hello @overhead_press2 ,

In the below steps I will show you how you can get all the Folders (including Sub Folders), then traverse each Folder to get Files within them which satisfies your condition and delete them.

  1. I am using a manual trigger, you can decide when do you want this Flow to execute
  2. Add a "Send an HTTP Request to SharePoint" action to get all the Folders including Sub Folders in a Library, you will need to pass
    1. Site Address = Select the Site where the Library is residing
    2. Method = GET
    3. Uri = _api/web/Lists/GetByTitle('Library Name')/Items?$expand=Folder 
    4. NiloferA_7-1620721720376.png
  3. Add "Filter Array" action to filter out only the Folders, pass the following parameters
    1. From = add Expression ['d']['results'] , then take your cursor to the beginning of the Expression, go to Dynamic Content and select body under Send an HTTP Request to SharePoint. Your final expression should look like this - outputs('Send_an_HTTP_request_to_SharePoint')?['body']['d']['results']
    2. NiloferA_2-1620720978788.png
    3. Choose a value (Left) = add Expression item()['FileSystemObjectType']
    4. NiloferA_4-1620721191922.png
    5. Condition = "is equal to"
    6. Choose a value (Right) = 1
    7. NiloferA_5-1620721263236.png
  4. Add an "Apply to Each" action and pass the Body from Filter Array 
  5. Inside "Apply to Each", add "Send an HTTP Request to SharePoint" action, configure as before and set the Uri as _api/Web/GetFolderByServerRelativeUrl('add Expression = item()['Folder']['ServerRelativeUrl'] ')?$expand=Files
    1. NiloferA_6-1620721686090.png
  6. Next add "Filter Array" action to filter out all the Files which start with a specific string
    1. From = add Expression outputs('Send_an_HTTP_request_to_SharePoint_2')?['body']['d']['Files']['results']
    2. Choose a value (Left)item()['Name']
    3. Condition = "starts with"
    4. Choose a value (Right) = any string you want to pass
    5. NiloferA_8-1620722068115.png
  7. Now to delete all the files returned by this Filter action, you will need to add another Apply to Each and add a Send an HTTP Request to SharePoint action yet again and configure it as follows where the expression in uri is item()['ServerRelativeUrl']
    1. NiloferA_9-1620723484009.png
    2. NiloferA_10-1620723587593.png

Hope this helps you out!

Please give a Kudo or mark this reply as Solution if you found it helpful.

 

View solution in original post

3 REPLIES 3
NiloferA
Responsive Resident
Responsive Resident

Hello @overhead_press2 ,

In the below steps I will show you how you can get all the Folders (including Sub Folders), then traverse each Folder to get Files within them which satisfies your condition and delete them.

  1. I am using a manual trigger, you can decide when do you want this Flow to execute
  2. Add a "Send an HTTP Request to SharePoint" action to get all the Folders including Sub Folders in a Library, you will need to pass
    1. Site Address = Select the Site where the Library is residing
    2. Method = GET
    3. Uri = _api/web/Lists/GetByTitle('Library Name')/Items?$expand=Folder 
    4. NiloferA_7-1620721720376.png
  3. Add "Filter Array" action to filter out only the Folders, pass the following parameters
    1. From = add Expression ['d']['results'] , then take your cursor to the beginning of the Expression, go to Dynamic Content and select body under Send an HTTP Request to SharePoint. Your final expression should look like this - outputs('Send_an_HTTP_request_to_SharePoint')?['body']['d']['results']
    2. NiloferA_2-1620720978788.png
    3. Choose a value (Left) = add Expression item()['FileSystemObjectType']
    4. NiloferA_4-1620721191922.png
    5. Condition = "is equal to"
    6. Choose a value (Right) = 1
    7. NiloferA_5-1620721263236.png
  4. Add an "Apply to Each" action and pass the Body from Filter Array 
  5. Inside "Apply to Each", add "Send an HTTP Request to SharePoint" action, configure as before and set the Uri as _api/Web/GetFolderByServerRelativeUrl('add Expression = item()['Folder']['ServerRelativeUrl'] ')?$expand=Files
    1. NiloferA_6-1620721686090.png
  6. Next add "Filter Array" action to filter out all the Files which start with a specific string
    1. From = add Expression outputs('Send_an_HTTP_request_to_SharePoint_2')?['body']['d']['Files']['results']
    2. Choose a value (Left)item()['Name']
    3. Condition = "starts with"
    4. Choose a value (Right) = any string you want to pass
    5. NiloferA_8-1620722068115.png
  7. Now to delete all the files returned by this Filter action, you will need to add another Apply to Each and add a Send an HTTP Request to SharePoint action yet again and configure it as follows where the expression in uri is item()['ServerRelativeUrl']
    1. NiloferA_9-1620723484009.png
    2. NiloferA_10-1620723587593.png

Hope this helps you out!

Please give a Kudo or mark this reply as Solution if you found it helpful.

 

View solution in original post

Thank you so much for the in-depth reply, one question is for step 5, how would I get the server specific url? Sorry I am relatively new to SharePoint, thanks! 

Hello @overhead_press2 ,

In SharePoint, there is a concept called ServerRelativeUrl which means a URL for a resource in SharePoint which starts with a "/" and excludes the domain name.

For e.g. Let's assume you have a file File1.xlsx in a SharePoint Library, the actual Path (Or absolute URL) to this File will be "https://yourdomain.sharepoint.com/sites/MySite/MyLibrary/File1.xlsx" and the Server Relative URL will be "/sites/MySite/MyLibrary/File1.xlsx".

Now going back to my Reply above, In Step 3 we are getting all the Folders including Sub Folders in the Library, when we send the http call to SharePoint, it returns a JSON Object which contains metadata for these Folders (Check the Output of Send http request to SharePoint action when you implement this solution) which includes ServerRelativeUrl for each folder.

We are then using this URL to query and get all the files within each of the Folders retrieved in Step 3.

I hope this answers your question, feel free to post any other questions you may have.

Helpful resources

Announcements
Process Advisor

Introducing Process Advisor

Check out the new Process Advisor community forum board!

MPA User Group

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group

MBAS on Demand

Microsoft Business Applications Summit sessions

On-demand access to all the great content presented by the product teams and community members! #MSBizAppsSummit #CommunityRocks

Users online (17,299)