Top

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.

6 Comments
  • Taylor Steele

    Hi,

    Are these flows still working for you? I noticed that the HTTP request has started only returning items where the “DeletedByEmail” field matches the account used for the connector.

    Cheers

    June 29, 2020 at 2:04 am Reply
  • Niko Van Beersel

    Hi Tomasz,
    I’m trying to reapply this method for when a list item is deleted instead of a file (see no reason why that wouldn’t work – hope you agree 🙂 ).
    I’m however not sure how to do the Parse JSON step… Can you explain what to write there in the Schema to get he Id correctly structured?

    July 10, 2020 at 6:17 pm Reply
    • Tomasz Poszytek

      Every time you’re in such “doubt” make a test run of your workflow and investigate what the trigger (or other action) returns. Then grab that outputs, it’s always a JSON and simply use it in “Parse JSON” action via the sample payload option.

      July 28, 2020 at 7:49 am Reply
  • Sarah Newman

    Hi Tomasz, I have found that if I call web/RecycleBin I get just my recycle bin and if I use site/RecycleBin I appear to get the admin recycle bin. Is that what you expect?

    September 9, 2020 at 1:40 am Reply
    • Tomasz Poszytek

      Good question. Web/Site in terms of Site Collection (not a regular subsite) should be the same thing.

      September 10, 2020 at 2:16 pm Reply

Post a Comment

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