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

How to delete all files in OneDrive created in the past 24 hours?

Hi guys,

 

So I'm brand new to PA. Just discovered it yesterday. So I've been testing it and playing around with it to fully get a feel of all the flows by creating different ones myself rather than templates.

 

In the process, I have created a very cluttered OneDrive that I need to clear up.

 

How can I create a flow where it'll simply delete all files from my OneDrive that were created in the last day (or any time range for that matter)?

 

I have tried using the 'move/replace a file' action, but it won't let me select a folder, only a file? I don't just need to delete one file (don't need a flow for that anyway), I need to filter by time and delete. For that, I also need to be able to select dynamic content (which it's not letting me either). Does anybody know how to do that? 

 

Here's my (very bad) attempt at creating this flow lol. Would appreciate it if someone could help.

 

powerautomatist_0-1625326888432.png

 

Many thanks!

2 ACCEPTED SOLUTIONS

Accepted Solutions
ccc333ab
Solution Sage
Solution Sage

Are all the files in a single folder...or are they all over the place? 

 

If in a single folder, the easy way to do this is just to loop through each file and test if the file date is greater than or equal to today's date minus one. 

 

So....

 

1. Get all files in folder: 

ccc333ab_0-1625333026765.png

2. Loop through each value, and then check if the Last Modified Date is greater than or equal to today's date subtract one. 

ccc333ab_1-1625333074819.png

 

Expression on right side of condition is: 

addDays(utcNow(), -1, 'yyyy-MM-dd')

 

Note: I just have a COMPOSE statement in my YES response as I don't want to really delete any of my files, but this is where you would add in your delete....

 

View solution in original post

ccc333ab
Solution Sage
Solution Sage

Solution for date range, pretty much following the same logic as the original solution: 

 

1. Setup your trigger to allow you to enter in your date range you want to use. 

ccc333ab_0-1627245847086.png

 

2. Get your list of files as per before.

ccc333ab_1-1627245877621.png

 

3. Now loop through the files, and check to see if the date last modified is greater than or equal to your start date, AND if the date last modified is less than or equal to your end date.  (Note: I always format my dates just to make sure they are of the same format just in case...). 

IF TRUE---> Then delete that file (using the ID of the current item) 

ccc333ab_3-1627246045936.png

 

The expressions in the Condition are: 

formatDateTime(items('Apply_to_each')?['LastModified'],'yyyy-MM-dd')
     is greater than or equal to
formatDateTime(triggerBody()['date'],'yyyy-MM-dd')


formatDateTime(items('Apply_to_each')?['LastModified'],'yyyy-MM-dd')
   is less than or equal to
formatDateTime(triggerBody()['date_1'],'yyyy-MM-dd')

Note: Where it says date and date_1 was named by the system when I created the two inputs. Make sure your values are the same (depending on if you have other inputs in your trigger). 

 

View solution in original post

8 REPLIES 8
ccc333ab
Solution Sage
Solution Sage

Are all the files in a single folder...or are they all over the place? 

 

If in a single folder, the easy way to do this is just to loop through each file and test if the file date is greater than or equal to today's date minus one. 

 

So....

 

1. Get all files in folder: 

ccc333ab_0-1625333026765.png

2. Loop through each value, and then check if the Last Modified Date is greater than or equal to today's date subtract one. 

ccc333ab_1-1625333074819.png

 

Expression on right side of condition is: 

addDays(utcNow(), -1, 'yyyy-MM-dd')

 

Note: I just have a COMPOSE statement in my YES response as I don't want to really delete any of my files, but this is where you would add in your delete....

 

powerautomatist
Frequent Visitor

.

Hi ccc333ab,

 

Thank you so much for your help.

 

I just tried this, and it ran successfully the first two times, but it didn't delete anything. Turns out I hadn't changed the condition to 'greater than'. I corrected it, then I got this. Not sure why.

 

powerautomatist_0-1625403863609.png

 

 

Do you think you could help? 

 

Thanks

ccc333ab
Solution Sage
Solution Sage

Sorry, but I cannot read the image you pasted, even if I enlarge it. Can you either type out what the message says or paste in a higher quality image. 

ccc333ab
Solution Sage
Solution Sage

One thought.

 

The Delete Item would throw an error if you are trying to delete any folders (as it only deletes files). And there is no "Delete Folder" currently in Power Automate.

powerautomatist
Frequent Visitor

Hi there,

 

I know this reply is a long time coming, but I finally got around to it.

 

Thank you for your reply, I did try the flow again. It ran successfully the second time, except nothing got deleted. Not sure why.

 

Since it's been a while, I now need a flow where I can specify a date range that I'd like to delete files from. Do you think that's an option? 

ccc333ab
Solution Sage
Solution Sage

Solution for date range, pretty much following the same logic as the original solution: 

 

1. Setup your trigger to allow you to enter in your date range you want to use. 

ccc333ab_0-1627245847086.png

 

2. Get your list of files as per before.

ccc333ab_1-1627245877621.png

 

3. Now loop through the files, and check to see if the date last modified is greater than or equal to your start date, AND if the date last modified is less than or equal to your end date.  (Note: I always format my dates just to make sure they are of the same format just in case...). 

IF TRUE---> Then delete that file (using the ID of the current item) 

ccc333ab_3-1627246045936.png

 

The expressions in the Condition are: 

formatDateTime(items('Apply_to_each')?['LastModified'],'yyyy-MM-dd')
     is greater than or equal to
formatDateTime(triggerBody()['date'],'yyyy-MM-dd')


formatDateTime(items('Apply_to_each')?['LastModified'],'yyyy-MM-dd')
   is less than or equal to
formatDateTime(triggerBody()['date_1'],'yyyy-MM-dd')

Note: Where it says date and date_1 was named by the system when I created the two inputs. Make sure your values are the same (depending on if you have other inputs in your trigger). 

 

Wow, you, sir, are a hero!

 

Thank you so much! I really appreciate your helpful responses, this is going to make my life much easier knowing these flows.

 

Thanks once again, and have a brilliant weekend! 🙂 

Helpful resources

Announcements
Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

New Ideas Forum MPA.jpg

A new place to submit your Ideas for Power Automate

Announcing a new way to share your feedback with the Power Automate Team.

MPA Virtual Workshop Carousel 768x460.png

Register for a Free Workshop

Learn to digitize and optimize business processes and connect all your applications to share data in real time.

Super User 2 - 2022 Congratulations 768x460.png

Welcome Super Users

The Super User program for 2022- Season 2 has kicked off!

Users online (2,619)