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 / Deleting any file with...
Power Automate
Answered

Deleting any file with last modified date value greater than 45 days from the root directory and all the subdirectories.

(0) ShareShare
ReportReport
Posted on by 5

Hello,

 

I have been researching this and testing this flow all day but could not figure it out myself.

 

I am trying to create a flow that will run once a day, list every single file from the root directory and all the subdirectories.

From the list, I would like to make sure each item is not a folder, and the item has a last modified time value greater than 45 days.

If the condition is met, I would like to delete that file.

 

I think the output I am putting into the "Apply to each" function is not correct. Could anyone help me get this thing to work?

djkim_0-1672783913980.png

djkim_1-1672784000179.png

 

Categories:
I have the same question (0)
  • Verified answer
    grantjenkins Profile Picture
    11,063 Moderator on at

    I'm assuming you mean you want to delete files that have a modified date older than 45 days (were last modified more than 45 days ago). I'd actually use the SharePoint connectors for this as you can filter the data directly when retrieving them which is much more efficient. OneDrive is effectively a SharePoint site behind the scenes so you can use any of the SharePoint connectors with OneDrive.

     

    IMPORTANT - since this is deleting files, I'd ensure it's returning the files you expect before adding the Delete file action. Once you've confirmed it's working as expected then you can add the Delete file connector. One way to do this is to add a Get file metadata instead of the Delete file and check the files being returned. Then when you are sure it's working as expected, remove the Get file metadata and add the Delete file.

     

     

    See full flow below. I'll go into each of the actions.

    grantjenkins_0-1672791000651.png

     

    Get files (properties only) retrieves files that are older than 45 days old.

     

    Site Address is the URL of your OneDrive account. You can click on Custom value and paste the URL in.

    Library Name is Documents which is your My Files library in OneDrive.

    Included Nested Items is set to Yes.

    Filter Query has two conditions:

    • FSObjType eq 0 ensures it only returns files (without this it returns both files and folders).
    • Modified lt '@{addDays(utcNow(), -45)}' ensures only files that have a modified date older than 45 days are returned.
    FSObjType eq 0 and Modified lt '@{addDays(utcNow(), -45)}'

    grantjenkins_1-1672791271686.png

     

    Apply to each iterates over all the files returned. Note that we don't need a condition within the loop as it's already filtered the files that we want to delete.

    grantjenkins_2-1672791381706.png

     

    Delete file uses your OneDrive URL the same as your Get files. And uses the Identifier to get the current file.

    grantjenkins_3-1672791443166.png

     

    Again, I'd ensure you build the flow to just return the files and check thoroughly that only the files you expect to see are being returned. Once you've confirmed it's working as expected then you can add the Delete file connector. One way to do this is to add a Get file metadata instead of the Delete file and check the files being returned. Then when you are sure it's working as expected, remove the Get file metadata and add the Delete file.


    ----------------------------------------------------------------------
    If I've answered your question, please mark the post as Solved.
    If you like my response, please consider giving it a Thumbs Up.

  • djkim Profile Picture
    5 on at

    Thank you @grantjenkins! Your solution makes a total sense.

  • CU06081659-0 Profile Picture
    122 on at

    Hi @grantjenkins ,

     

    I was looking for this solution and has worked for me successfully. Many thanks. What if I want to exclude 2 folders called "TEST 1" and "TEST 2" along with all its contents?

     

    Already Tried but none has worked:
    1. (FileLeafRef ne 'TEST 1')
    2. not (startswith(Title, 'TEST 1')
    3. not (endswith(Title, 'TEST 1')
    4. not (substringof('TEST 1',Path)
    5. not(folderPath ne '/TEST 1')
    6. or startsWith(FSObjDisplayPath, '/TEST 1/')


    Can you help me with revised formula?

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 501 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 323 Moderator

#3
abm abm Profile Picture

abm abm 237 Most Valuable Professional

Last 30 days Overall leaderboard