How to Delete Files Older than 3 Years from SharePoint Document Library Using Power Automate?

One of our clients recently required us to use Power Automate to delete SharePoint document library files older than 3 years.

In this tutorial, I will explain how to delete files older than 3 years from SharePoint Document library using Power Automate.

Also, we will discuss how to delete folders and subfolders (including files) from the SharePoint library using Power Automate.

Delete Files Older than 3 Years from SharePoint Document Library Using Power Automate

To delete all the files in the SharePoint document library older than 3 years, follow the below-given steps:

I have created a SharePoint document library [Course Documents] with Folders, files, Subfolders, and files inside folders, sub-sub folders, etc, as shown in the screenshot below:

Power Automate delete files in folders in SharePoint document library

Next, follow the steps below:

1. Navigate to the Power Automate Home page, click + Create -> select the Instant cloud flow -> provide the flow name -> choose the flow trigger (Manually trigger a flow) -> click the Create button.

2. Next, take ‘Get files(properties Only)‘ flow action. Provide required parameters:

  • Site Address: Choose a specific site address from the drop-down menu.
  • Library Name: Select a library name under the SharePoint site.

Click on Show all to display all the Advanced parameters.

To retrieve files older than 3 years, we have to provide the filter query expression so that it will get all the files and folders that meet the expression.

  • Filter Query: Insert the below-given code.
TestDate lt '@{subtractFromTime(startOfMonth(utcNow()),3,'Year','MM-dd-yyyy')}'

Note:

To get the SharePoint document library files older than 3 years, Replace the above expression with Created:
Created lt ‘@{subtractFromTime(startOfMonth(utcNow()),3,’Year’,’MM-dd-yyyy’)}’

To get the files present in all the folders and subfolders, include the below parameter:

  • Include Nested Items: Select Yes from the drop-down.
How to delete a file from SharePoint document library using Power Automate

Important:

See also  How to Use Microsoft Copilot in Power Automate?

To delete the files present in a single folder of the SharePoint document library, check the below parameter:

Limit Entries To Folder: Select a folder name from the Open folder icon from where you want to delete the files.

Power Automate flow to delete a file in SharePoint library

Now, I wanted to delete only SharePoint document library files, So I will add a Condition where it will only extract the files from the library.

3. Take a Condition control flow action and set the parameters as below:

Choose ValueOperatorChoose Value
@items(‘For_Deleting_Files’)?[‘{IsFolder}’]is equal tofalse

Each loop will be added automatically by taking body/value of Get files properties flow action.

Delete Files in SharePoint document library using Power Automate

4. Now, add a Delete item flow action under the True condition. Configure the parameters:

Note:

We can add Delete item flow action to delete all the files and folders directly based on ID, instead of Delete File flow action in Power Automate.
  • Site Address: Choose a SharePoint site name from the drop-down.
  • List Name: Enter the library name manually.
  • Id: Select ID under Get files(properties only) from dynamic content.
Power Automate delete all files in SharePoint Library folder

5. Now, the flow is ready. Click on the Save icon and run it manually.

6. When the flow executes successfully, it will delete all files in the SharePoint document library and all files in SharePoint folders.

You can refer to the image below:

Power Automate delete all files in SharePoint document library folder
Power Automate delete files older than 3 years

What to do if the SharePoint Library has More Files?

Power Automate Get file (properties only) flow action can retrieve the files up to the list view threshold limit of 5,000.

When working with a large list, to get more than 5000 files, we have to enable the Pagination option in the Get files.

See also  Power Apps Cascading Dropdown Control

Pagination Settings:

Inside the Get files flow action -> Click on Settings -> Under Networking, toggle the Pagination to On -> Provide the Threshold items to return.

Power Automate to delete files in SharePoint Document library

Important:

We can set the threshold limit to 100000 (the maximum limit) in a manually trigger flow but it does not allowed in automated flow.
If a user who triggers the flow has a premium license, then can be able to extend the pagination to 100000 else also use a Do Until control to loop through all items in the SharePoint list.

This way, you can delete files and files in folders from the SharePoint document library using Power Automate.

Power Automate Delete Folders in SharePoint Document Library

To delete folders from the SharePoint document library using Power Automate, check the below steps:

Here, I will use the same document library mentioned in the above example.

Power Automate delete folders in SharePoint document library

1. Create an Instant cloud flow.

2. Include ‘Get files (properties only),’ provide the properties like Site Address, List Name, and Filter Query as described previously.

  • Include Nested Items: Select No from the drop-down.
Power Automate delete SharePoint folders older than 3 years

3. Provide a condition to delete only SharePoint document library folders. Add a Condition control flow action. Set the details:

IsFolder is equal to true
How to delete a folders from SharePoint document library using Power Automate

4. Under True condition, select a Delete item flow action and set the details like Site Address, List Name, and Id as shown in the image below:

Power Automate delete all folders in SharePoint document library

Save and Test the flow manually.

Output:

As you can see, the flow runs successfully and deletes the document library folders in SharePoint.

How to delete SharePoint folders and subfolders using Power Automate

This is all about how we can delete folders in the SharePoint document library using Power Automate.

See also  How to Check If a Date is Blank in Power Automate?

Conclusion

I hope this tutorial gave you an idea of how to delete SharePoint document library files, files inside folders, and folders using Power Automate.

Some more articles you may like:

>