SOLVED

Deleting Files Automatically after 3 Months

Copper Contributor

Hello, 

 

Is it possible to create a flow that will automatically delete files in specific SPO folders after 3 months? I have tried using a flow, but I am not experienced with them and am having trouble.

 

Thanks. 

 

 

15 Replies
best response confirmed by VI_Migration (Silver Contributor)
Solution

@Filmanac yes this can be done with a simple flow in Power Automate as shown below.

 

1. Your trigger needs to be a recurrence schedule which you set to 1 day (and the timezone and time you want it to run) so that it checks the library every day:

1-Flow.png

 

For the first action select the SharePoint get files (properties only), select your site and the Document library. But you only want to bring back those items that were created over 3 months ago, the rest you can ignore. So in the Filter Query field add the following:

Created lt '{expression}addDays(utcNow(), -90){/expression}'

 

(Created is the column that is being checked, lt = less than, utcNow() = today, -90 is 90 days ago).

 

Don't forget to put the expression inside single quotes!

 

2-Flow.png

 

It will default to bringing back items from the entire document library but you can limit it to a specific folders by clicking the folder icon on the right and selecting the folder:

2a-Flow.png

 

Finally, add a SharePoint delete file action. It will automatically wrap itself in an apply to each (as the previous action was get items). Select Identifier from the dynamic content box. The items that were brought back by the get items will then be deleted.

 

3-Flow.png

 

Rob
Los Gallardos
Microsoft Power Automate Community Super User

Hello Rob,

Thank you for the instructions on this. It made everything much clearer. The flow runs successfully in both test and when left to itself based on the time margin, however, files older than 3 months/90 days are not being deleted.

I have made sure to include nested items, so not sure what is stopping things now. I will need to dig a bit deeper.

Thank you for your help so far though.
If you post a screenshot of your flow then we can see where you've gone wrong.

@RobElliott I have attached two screenshots of the flow. I haven't included it in the screenshot, but the code I'm using for the filter query is 'addDays(utcNow(),-90). 

 

I have removed the actual SharePoint site address and the specific folder I'm using.

@RobElliott Thanks for this This flow doesn't seem to work for me, and I'm not entirely sure why, but it seems like it is looking for anything created exactly 3 days ago (I need things deleted more than 3 days old). So I've used Created It 'addDays(utcNow(), -3)', but I keep getting this error.

error.png

 

Am I missing something obvious?

@Filmanac Power Automate is a good option if you need to do more actions before or after deleting the docs. If you don´t have a complex scenario you could create a retention policy in the document library. See attached images. You will need to activate the site collection feature called "Site policy" in order to use this functionality.

@Maggan_W 

 

I know you weren't replying to my query, but about this solution, as I'm giving it a try to see if it works for my problem. I can't see how you can apply this to individual folders from within a document library. Doesn't it apply to the library as a whole?

@LCSilliman, First, create a custom folder content type for your specific purpose. Make sure that your folder is using this content type. Then, open the Information management policy settings. You will now have the option to select your custom FolderCT and apply a retention policy. This would apply to all folders using this content type, which can be very useful in some cases.

@LCSilliman did you manage to solve your issue with the query failed ?
Am running in to the same problem!
@RobElliott, thanks for this!
But how to delete folders?
all files as well as in sub folders are deleted but the folders are still remaining.

@Dan0365 sorry, I've only just seen your post. There is no action in Power Automate to delete a folder but you can vote for it here.

 

Rob
Los Gallardos
Microsoft Power Automate Community Super User

Thanks for replying, Iv voted for this feature!
On a related note, my script failed this morning with the error that a subfolder did not exist, even tho it does and there are files inside that are out of date and should be deleted.
its been running ok for the past month, but i think today is when files now out of date.

Any idea why its showing not existing ?

@Dan0365 @LCSilliman  I'm also running into this error. Any advice? Were you able to get it resolved? Thank you!

@anniewendel I've had no resolution to this issue, but it was because I don't have the appropriate rights in the enterprise version my workplace is using.

@anniewendel am afraid not and have ended up changing how users use the folder.
1 best response

Accepted Solutions
best response confirmed by VI_Migration (Silver Contributor)
Solution

@Filmanac yes this can be done with a simple flow in Power Automate as shown below.

 

1. Your trigger needs to be a recurrence schedule which you set to 1 day (and the timezone and time you want it to run) so that it checks the library every day:

1-Flow.png

 

For the first action select the SharePoint get files (properties only), select your site and the Document library. But you only want to bring back those items that were created over 3 months ago, the rest you can ignore. So in the Filter Query field add the following:

Created lt '{expression}addDays(utcNow(), -90){/expression}'

 

(Created is the column that is being checked, lt = less than, utcNow() = today, -90 is 90 days ago).

 

Don't forget to put the expression inside single quotes!

 

2-Flow.png

 

It will default to bringing back items from the entire document library but you can limit it to a specific folders by clicking the folder icon on the right and selecting the folder:

2a-Flow.png

 

Finally, add a SharePoint delete file action. It will automatically wrap itself in an apply to each (as the previous action was get items). Select Identifier from the dynamic content box. The items that were brought back by the get items will then be deleted.

 

3-Flow.png

 

Rob
Los Gallardos
Microsoft Power Automate Community Super User

View solution in original post