Restore removed files in SharePoint with Power Automate

Having the trigger in Power Automate called “When a file is deleted” many asks if there is a possibility to restore that removed file or delete it permanently? Yes it is. Let me show you how.

RecycleBin object

The best way to accomplish this is to use SP.RecycleBinItem object from SharePoint’s Core Objects’ Library (more). The object has two methods:

  • deleteObject – removes item from the recycle bin permanently
  • restore – restores the item from the recycle bin

Both methods can be called via REST endpoints:

  • http://<sitecollection>/<site>/_api/web/RecycleBin(recyclebinitemid)/deleteObject()
  • http://<sitecollection>/<site>/_api/web/RecycleBin(recyclebinitemid)/restore()

Note: if you are building the solution for the web that is a root of the site collection or for the modern team/ communication site, then replace web with site, to get access to the site recycle bin.

The key challenge to use them is: how to obtain the “recyclebinitemid“. Let me show you how.

When a file is deleted

This trigger is fired once an item is deleted in SharePoint (source). The action has a very limited number of outcomes and they are somehow not very much useful:

  "ID": [SharePoint Library Item ID],
  "Name": "[Display name]",
  "FileNameWithExtension": "[Display name with extension]",
  "DeletedByUserName": "[Name and lastname of the user]",
  "TimeDeleted": "[Timestamp]",
  "IsFolder": [true/false]

The information scope we can get about each deleted file, as we call http://<sitecollection>/<site>/_api/web/RecycleBin endpoint is much wider. What is missing in data returned from trigger is information about the path, under which the file was originally present and recycle bin item id, that we can use to restore or permanently delete file. How to get it? See below.

Recycle Bin Item ID

The way you can get it is the following:

  1. Call http://<sitecollection>/<site>/_api/web/RecycleBin endpoint using Send an HTTP request to SharePoint action.
  2. Filter returned list to leave only the item that matches deleted item name and extension using Filter array action.
  3. Parse returned JSON.
  4. Restore the item using Recycle Bin Item ID, using Send an HTTP request to SharePoint action.

In step no. 2 use: body('Get_files_from_Recycle_Bin')['d']['results'] for “From” and item()['Title'] is equal to triggerBody()?['FileNameWithExtension'] for filter.

Then parse the item’s JSON and use body('Parse_JSON')['Id'] as a parameter in Uri to restore the item: _api/site/RecycleBin('@{body('Parse_JSON')?['Id']}')/restore().

Why is it useful?

This simple, but important functionality can help you in governing SharePoint files, which is the thing now. Having the possibility to remove a file is a privilege’s of most users, however adding additional layer to review if the file can be actually removed and then eventually restore it is another case.

Also, for all those scenarios where you need to sync data between libraries or SPO and your on-prem world, this trigger is also very useful.

If you have any questions, leave them in comments below. Thanks!

Tomasz Poszytek

Hi, I am Tomasz. I am expert in the field of process automation and business solutions' building using Power Platform. I am Microsoft MVP and Nintex vTE.


Post a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.