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.

18 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
  • Majing

    I wonder how can I empty the recycle bin by http call?

    March 26, 2021 at 4:06 am Reply
  • Majing

    is there a way to delete/empty the recycle bin by API?

    March 30, 2021 at 7:45 am Reply
  • Ulf

    Hey Tomasz,

    two questions:

    1)Why you able to filter directly the response of the GET Request? In my case he tells me the response is an object and not an array…

    2)I need a information about a specific property of the deleted file to use it in the further process. Can you give me a hint how to get a property of a deledete file?

    April 23, 2021 at 10:45 am Reply
    • Tomasz Poszytek

      I am filtering d.results from the response body, not the body itself. Response contains table. As per second questions – as long the file is in recycle bin, you cannot get any metadata it used to have as it was in original location.

      April 28, 2021 at 11:32 am Reply
  • DARKLING

    Hi Tomasz.

    When calling https://******cloud.sharepoint.com/sites/****/_api/Web/recyclebin

    I only see the authenticated user deleted files. The account is admin so how do I see all deleted files for all users?

    Regards

    May 12, 2021 at 1:12 pm Reply
    • Tomasz Poszytek

      Yes, the workflow should work in a context of specific user so therefore has access to that user’s removed files. You can as well try to use “_api/site/RecycleBin” – once file is moved from web level, there you can access all files.

      May 12, 2021 at 2:52 pm Reply
  • Fernando Domínguez Martín

    How to force or overwrite this operation if same folder or item name exists in destination? It cause errors and no restore

    June 17, 2021 at 9:56 pm Reply
    • Tomasz Poszytek

      This is not possible. API doesn’t deliver such a functionality. In this case I’d say that if flow fails because of this reason, build logic that deletes/ renames existing file and then attempts to restore it again.

      June 30, 2021 at 3:17 pm Reply
  • JV

    Hi Tomasz, I’m encountering an issue where when sending the HTTP request to restore the file, I get a returned code of 401 saying UNAUTHORIZED. I’ve tested that I’m able to manually restore the exact same file from the recycle bin using the SharePoint UI, and I’ve also been able to perform some other SharePoint API calls in other Power Automate flows that use POST methods. Any idea what I’m doing incorrectly?

    July 5, 2021 at 9:47 pm Reply
    • Tomasz Poszytek

      Maybe you are trying to restore a file from a site collection bin, not the web one? Do you have site collection admin permissions or could you grant yourself and check again?

      July 15, 2021 at 9:56 am Reply

Post a Comment

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