Deleting files is a dangerous operation but important regarding automation and keeping things under control. One example is temporary files. You may need a file to be created during the Flow’s run, but since you don’t need it after the Flow is finished, it’s a good practice to delete it so as not to create “junk” files. There are other usages so let’s explore the “Delete file” action and how it can help us in our Flows.

Where to find it?

You can find it under “Standard”.

You may need to expand.

Pick “OneDrive for Business”.

Pick the “delete file” action.

Here’s what it looks like:

Pro Tip:
Power Automate tends to save the most common actions in the main screen, so check there before going through the full hierarchy. Also, you can use the search to find it quickly.

Now that we know how to find it let’s understand how to use it.

Usage

It’s pretty simple to use since we only need to provide the unique file identifier. For example, you can get the identifier from many actions like the “Create File” or the “Get file metadata using path” action. We’ll go into detail in the “Non-intuitive behaviors,” but please note that we need the file id and not the file path; even if you pick it from the UI, it presents the file path. Microsoft did this since it’s a lot easier to understand what file is being deleted if we see the path than a random ID, but be aware of this.

That’s it. The file is deleted once the Flow runs.

Non-intuitive behaviors

There’s a behavior that tricks people when using the “delete file” action, and it’s what to provide to the “File” field. Since it’s called “File”, we could think about the file itself (meaning its contents) or the path to the file. Power Automate even presents the file to use as a nicety.

The way it’s presented makes sense, but if you want to provide your value, you need to give the file’s ID, not its path, even if what you see above is the path. Power Automate does the translation from the path to ID behind the scenes, but only if you pick it from the UI.

Let’s see the issue in action by having the same path as above in a “Compose” action.

Here’s the result:


We get:

The file id '/TMP/Test File.xls' is not valid.

Please be aware of this since I see this happening a lot.

Limitations

You cannot delete locked file. This is not a limitation per se, but it’s something to keep in mind if you get a 423 “Forbidden” error. OneDrive for Business can lock files for several reasons, like if Power Automate edits the file in other actions of the Flow, so be careful. The standard strategy is to use a “Delay” action for a few seconds to let OneDrive “catch up,” update the status and close the file before deleting it.

Recommendations

Here are some things to keep in mind.

Name it correctly

The name is super important in this case since we’re deleting the file by either providing the ID or having a defined static one. Always build the name so that other people can understand what you are using without the need to open the action and check the details.

Always add a comment

Adding a comment will also help avoid mistakes. Indicate where the ID comes from, for example, so that the person looking understands why the file is being deleted and if it’s the correct file. This is also important in case the file’s path is being defined. It’s essential to enable faster debugging when something goes wrong.

Always deal with errors

Have your Flow fail graciously and notify someone that something failed. It’s horrible to have failing Flows in Power Automate since they may go unlooked-for a while or generate even worse errors. I have a template that you can use to help you make your Flow resistant to issues. You can check all details here.

Am I missing something? Leave a comment or interact on Twitter. Let’s work together to make this reference as complete as we can

Back to the Power Automate Action Reference.

Photo by Devin Avery on Unsplash

 

Manuel Gomes

I have 18 years of experience in automation, project management, and development. In addition to that, I have been writing for this website for over 3 years now, providing readers with valuable insights and information. I hope my expertise allows me to create compelling, informative content that resonates with the audience.

View all posts by Manuel Gomes →

2 thoughts on “Power Automate: OneDrive For Business – Delete file action

  1. Thank you Manuel, I always enjoy your well written articles. It is good and sometimes important to avoid having multiple files with the same name for some flows. I’ve had some success sending an HTTP request to delete a locked Excel file in Sharepoint, but haven’t seen any information about doing the same action for Onedrive Excel files.

  2. Hi Manuel,
    Delete file in One Drive fails “The resource you are attempting to access is Locked”.

    I have delayed for One minute before attempting to delete.

    I have used the id to select the file.

    Is there a solution to this ?

    Regards

    Nigel

Leave a Reply

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