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:
addDays(utcNow(),-15)
//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:
length(body('List_files_in_folder_2')?['value'])
//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!
*This post is locked for comments