Showing results for 
Search instead for 
Did you mean: 

Delete files older than ‘X’ days from OneDrive using Flow

In this article, I will show you how to setup the flow configuration to delete files older than ‘X’ days from OneDrive. This looks like an easy task by simply listing the entire folder and set a condition to check the modified date and delete the older files. The catch here is, the List Folder action on OneDrive gets you 20 items only. There is no way (pagination settings are not working either) to get more than 20 items.

So here’s the high level architecture of this flow: List Files from a folder and check the modified date. If the file is older than ‘X’ days, delete it and if not, move it to a temporary folder. Now keep doing this until there are no more files left in the folder. Note that we are doing this just for files in a folder not folders inside a folder. Lastly, all the files that were moved to the temporary folder will all be restored to the base folder.

Let’s get started!

Trigger: Recurrence: I am using the recurrence trigger to trigger the flow everyday at 8 AM.

Action: Initialize Variable: Initialize an integer type temporary variable to store the number of files returned from the List Files from a Folder action. Initially the value for this variable is set to 100 (It is just a temporary value can be set to any integer other than 0)

Action: Initialize Variable 2: Initialize an integer type temporary variable to store the count of folders returned from the List Files from a Folder action. Initially the value for this variable is set to 0 (mandatory) and will be incremented every time a folder is found in the list returned.


Control: Do Until: This control is defined to work until the number of files returned from the List files in a folder action is equal to the number of Folders encountered. 

//Do until loop starts// 

Action: List files in a Folder: Select the folder you want to perform these actions on. I am using the root folder in this action.

Control: Apply to each: to iterate over each list item returned from the above action. 

//Apply to each loop starts//

Control: Condition 2: To check if the current item is a Folder. 

//Yes branch condition 2//

This is if the current item is not a folder. 

Control: Condition: To check if the last modified of the current item is older than 'X' days (I am doing it for 15 days). The expression used here is: 


//Yes branch condition//

Action: Delete file: Provide the ID of the current item from the dynamic selector to delete this file. 

//No branch condition//

Action: Move or rename a file: If the current file does not meet the condition, it is being moved to a temporary folder.

//No branch condition 2//

Action: Increment variable: If the current item is a folder, increment the folder count variable by 1. 

//Apply to each loop ends//


Action: List files in a Folder 2: To get the fresh list from the OneDrive document library

Action: Set Variable: The value of the temp variable to store the count of items returned in the above action is set in this action. The expression used here is: 


//Do until loop ends//


Action: Initialize a variable 3: Initialize an integer type variable to store the count of files in the temporary folder.

Control: Do until 2: This control is to run until all the files in the temporary folder are moved to the base folder. The variable value is being checked and it will run until there are no more files in the temporary folder. 

//Do until 2 loop starts//

Action: List files in a Folder 3: To list the files in the temporary folder.

Control: Apply to each 2: Iterate over each item returned in the list as an output from the above action. 

//Apply to each 2 loop starts//

Action: Move or rename a file 2: move the current file from the temporary folder to the base folder.

//Apply to each 2 loop ends//

Action: List files in a folder 4: Again list the files from the temporary folder and store the count in the variable. 

Action: Set variable 2: to store the count of the number of files returned in the list from the output of the above action. 

//Do until 2 loop ends//


In this flow, I showed you how to use the Do Until control to loop and get a set of items perform certain operations on the retrieved items and keep doing that until all the conditions on all the items for that connector are met. Details like file name, created by and created on etc. of the deleted files can be captured in an HTML table and further sent out as an email to the relevant users. Note that this configuration will only work until 20 Folders are encountered. If you have so many folders, you can tweak the flow a little bit by moving the folders to another folder and continue with the actions.

To reduce this tedious process and increase the limit on the number of items returned in the List files from a folder action, upvote ideas created here to include fillter queries, top counts etc. on the List files in a Folder OneDrive action. 

I hope you found this interesting and it helped you. Thank you for reading!

Meet Our Blog Authors
  • Working daily with Microsoft Cloud to deliver the needs of my company, my customers and various Microsoft communities and forums. | Office 365 | Flow | PowerShell | PowerApps | SharePoint |
  • Co-founder of, Office 365 and SharePoint expert. Passionate about design and development of easy to use, convenient and flexible products.
  • Microsoft Business Apps MVP. Owner of ThriveFast, an Office 365 consulting company.
  • 7x Microsoft Business Solutions MVP (CRM)
  • I'm keen in MS technologies, SharePoint, Office 365 and development for them
  • Daniel is a Business Productivity Consultant & Microsoft Business Solutions MVP who is very enthusiastic about all things Office 365, Microsoft Flow, PowerApps, Azure & SharePoint (Online). Since the preview, Daniel has been working with Microsoft Flow and later on with Microsoft PowerApps. That led to him being awarded an MVP Award for Business Solutions. He loves to blog, present and evangelize about improving productivity in the modern workspace with these amazing tools!
  • Michelle is an Office 365 solution architect in Twin Cities, MN. She has been delivering business collaboration solutions for years with her focus on SharePoint and Office 365. Michelle is a recent board member of the Minnesota Office 365 User Group and has been a member of the SharePoint community since 2009. She is a frequent speaker at MNSPUG and SharePoint Saturday and co-chaired the Legal SharePoint User Group for 4 years. Her most frequent projects have involved rolling out a large deployment of Office 365, SharePoint Online intranet, build of a "CHAMPS" Office 365 user adoption program and most recently, SharePoint On-Premise to Online Migration. Michelle is very excited about cloud technology as it is shifting her IT Pro focus to collaboration strategy and technical adoption.
  • I'm a Microsoft Office Servers and Services MVP with a special interest in SharePoint, Office 365, Microsoft Flow, Microsoft Teams and PowerApps. I work at Triad Group Plc (
  • Passionate #Programmer #SharePoint #SPFx #Office365 #MSFlow | C-sharpCorner MVP | SharePoint StackOverflow, Github, PnP contributor