“I know how to handle new and modified files, but what about the deleted ones, how do I remove them from a synchronised library using Power Automate flow?”
To keep two libraries synchronised you must handle three types of events. Create new files, modify existing files, and remove deleted files. The first two situations were already described in a previous post, but what about the third one? How do you delete the corresponding file from the second library?
Trigger flow on file deletion
While creation and modifications can be in a single flow, deletion needs its own flow. It’s because it needs a different trigger action – ‘When a file is deleted’.
Now you’ve got two options. If the file name is always unique, you can look just for the file name. But if you have files with the same name in different folders, you’ll need also the file path.
Option A – unique file names
If all the files in the document library have a unique file name, you can use it to search for the file. Add the ‘Get files (properties only)’ action with a Filter Query on the file name.
FileLeafRef eq '<file name with extension>'
The file names are unique so it’ll return only a single file – delete it using its Identifier.
Option B – duplicate file names in different folders
If there’re files with the same name in different folders, you can’t use the file name. The action would return more files and you shouldn’t delete them all. You must find only the file that was deleted among them.
Since the trigger action won’t give you the location of the deleted file, you must find it yourself. That means finding all the files with the same file name in both of the libraries and searching for the one that’s missing. Add two ‘Get files (properties only)’ actions with the same Filter Query.
For each result add the ‘Select’ action to select only the folder paths.
As the file was deleted only in the first library, it still exists in the second one. And that’s what you must find, which path is still in the second array while missing in the first one?
Add ‘Apply to each’ to loop through all the paths in the second array (you must loop through the bigger array). For each of them check if the path exists in the first array using a ‘Condition’.
If it’s true, the path is in both the arrays – the file still exists, do nothing. Otherwise get the deleted file properties using the ‘Get file metadata using path’ action. You have the file path (the currently processed item) and the full file name so you can build the full path.
Use the returned file Id to delete that file.
Summary
As you can see, there’re two very different ways how to manage deleted files in synchronised SharePoint library using Power Automate flow. If you have a simple library, where all files have a unique name, you can just search for the file name and delete the file.
But it’s not that easy if you use folder structure with duplicate file names. If there’s a possibility of multiple files with the same name in different folders, you can’t just search for all the files and delete them. You must find the right one, on the right path, using the information provided by the trigger action…
Hi,
This works great, except when there is a file with the same name in a subfolder. ie:
Folder1/file1.txt
Folder1/Folder2/file1.txt
In this case, when Folder1/Folder2/file1.txt is deleted, both will be deleted in the target library.
Hello Lucas,
that’s what the Option B is about, using FileLeafRef it’ll search for a full path of the file.
Works fine, but how about deleting an entire folder? would a similar logic using a “when an item is deleted” work?
Hello Adrian,
I think it should work, but since it’s a library you might need a ‘When a file is deleted’ trigger.