Let's POWER Automate

From no-code to low-code

Menu
  • Expressions
  • Filters
  • General
  • Application specific solutions
    • Dataverse
    • Excel
    • Forms
    • Outlook
    • Planner
    • Power Apps
    • SharePoint
    • Teams
  • Triggers
  • Ready to use solutions
    • Approval Process Template
    • Task Delegation App
    • The Ultimate Power Automate expressions cheat sheet
    • Power Automate HTTP requests to SharePoint cheat sheet
    • Power Automate HTTP requests to Graph API cheat sheet
  • ABOUT ME
  • Get help with your flow
  • POWER PLATFORM SERVICES
Menu

Handle deleted files in synchronised SP library in Power Automate

Posted on July 31, 2022July 31, 2022 by Tom

“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.

Power Automate synchronised library deleted files

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’.

Power Automate synchronised library deleted files

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.

Power Automate synchronised library deleted files

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…


πŸš€ Master Power Automate

Join 2,000+ professionals getting actionable Power Automate tutorials, solutions, cheat sheets & tips every week.

No spam. Unsubscribe anytime.

4 thoughts on “Handle deleted files in synchronised SP library in Power Automate”

  1. Lucas says:
    April 13, 2023 at 9:42 pm

    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.

    Reply
    1. Tom says:
      May 1, 2023 at 3:40 pm

      Hello Lucas,
      that’s what the Option B is about, using FileLeafRef it’ll search for a full path of the file.

      Reply
  2. Adrian says:
    May 10, 2023 at 9:55 pm

    Works fine, but how about deleting an entire folder? would a similar logic using a “when an item is deleted” work?

    Reply
    1. Tom says:
      May 28, 2023 at 3:05 pm

      Hello Adrian,
      I think it should work, but since it’s a library you might need a ‘When a file is deleted’ trigger.

      Reply

Leave a Reply Cancel reply

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

πŸš€ Master Power Automate

Join 2,000+ professionals getting actionable Power Automate tutorials, solutions, cheat sheets & tips every week.

No spam. Unsubscribe anytime.

Still exchanging emails to get things approved?

Use the Approval Process Template and the Task Delegation App to skip the hard part and deploy an automated, fully functional approval solution on a SharePoint list in minutes! And then the next one, and the next one...

Approval Template Preview ✨ Learn more ✨

Turn ideas into flows with ease!

Grab the complete Power Automate Cheat Sheet Bundleβ€”everything you need to master expressions, SharePoint HTTP calls, and Graph API in Power Automate.

Cheat Sheet Bundle Preview ✨ Get the Cheat Sheets

Didn't find what you were looking for?
Need to adjust a solution to fit your needs?
Or would you just like to get an assistance from somebody with thousands of hours of experience with Power Automate?

Power Automate blogs worth visiting

Damien Bird
Dennis (Expiscornovus)
Paul Murana

©2025 Let's POWER Automate | Theme by SuperbThemes