Pages

Microsoft Flow (delete files and folders from a document library)

Do you know how to delete files and folders from SharePoint Online document library using Microsoft Flow?

When I received the task to delete files and folders older than 30 days from SharePoint Online with Microsoft Flow I was thinking this will be an easy one, but I was wrong 😒

I used Recurrence as a trigger (run every 30 days), Get items, Apply to each and Delete item actions to select all files and folders older than 30 days:


But there is a problem with this configuration if you have folders with files in the document library. Get items will collect all folders and files, each and every one of them are having unique internal ID. It will delete folders with the files than all other files that are older than 30 days and it will try to delete the files from the folder(s) which are already deleted when the folder(s) has been deleted and it will throw and error: 404 item not found. The flow will fail but all the files and folders will be deleted.

How to avoid this error? First we need to get all the files and all the folders older than 30 days.


Get items (only files) has the following configuration (first you need to enable the Filter Query from Advanced parameters):
  • Created date should be less than addDays(utcNow(), -30, 'yyyy-MM-dd') and FSObjType equal 0 (0 is fo files and 1 is for folders)

After you have all the files with created date older than 30 days you apply for each action and delete them.

Last step is to select only folders older than 30 days and delete them, the setup looks like the screenshot below:


Hope this article help you solve this issue. Please share your feedback.

No comments:

Post a Comment