How to clear annoying Excel file locks in Power Automate

I was recently working on a project where I needed to use Power Automate to upload an Excel file to SharePoint, crack it open to get to the goodies inside and then delete the file.

Getting to the file contents is straightforward enough using the Excel connector, but the really annoying thing is the file sharing lock the Excel connector then places on a file which typically results in an angry red dot of failure when using the SharePoint delete file action:

The file https://company.sharepoint.com/teams/path/to/file.xlsx is locked for shared use by paul@company.com [membership].
clientRequestId: blah
serviceRequestId: blah;blah

I am sure many folks have hit this and might put a wait in their flow or write a scheduled flow to cleanup later when the lock has expired.

But did you know that we can easily bypass this issue and delete this locked file? All we need to do is use the very versatile swiss army knife action known as “Send an HTTP request to SharePoint” to call the API that handles recycling of files. We then add a directive to the header of the request that essentially says “I don’t care about your stupid lock, delete anyway!”

Here is what the API call looks like:

_api/web/Lists/GetByTitle('DocLib')/GetItemById(ID)/recycle

The extra magic is in the header:

Prefer: bypass-shared-lock

So the entire request looks like this:

Thanks for reading

Paul

Leave a Reply

Your email address will not be published. Required fields are marked *

*

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