Skip to main content

Notifications

Community site session details

Community site session details

Session Id : ZpKnKEEsrfhUSUDExilqHZ
Power Automate - Process Mining
Answered

Auto delete files in SharePoint subfolders based file name

Like (0) ShareShare
ReportReport
Posted on 11 May 2021 02:59:50 by 15

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! 

Categories:
  • NiloferA Profile Picture
    on 11 May 2021 at 17:01:03
    Re: Auto delete files in SharePoint subfolders based file name

    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.

  • overhead_press2 Profile Picture
    15 on 11 May 2021 at 15:13:43
    Re: Auto delete files in SharePoint subfolders based file name

    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! 

  • Verified answer
    NiloferA Profile Picture
    on 11 May 2021 at 09:03:27
    Re: Auto delete files in SharePoint subfolders based file name

    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.

     

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

Understanding Microsoft Agents - Introductory Session

Confused about how agents work across the Microsoft ecosystem? Register today!

Markus Franz – Community Spotlight

We are honored to recognize Markus Franz as our April 2025 Community…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 146,668 Most Valuable Professional

#2
RandyHayes Profile Picture

RandyHayes 76,287 Super User 2024 Season 1

#3
Pstork1 Profile Picture

Pstork1 66,004 Most Valuable Professional

Leaderboard
Loading started