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:
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.
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.
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:
The file id '/TMP/Test File.xls' is not valid.
Please be aware of this since I see this happening a lot.
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.
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.
Back to the Power Automate Action Reference.
Photo by Devin Avery on Unsplash