Skip to content

Let's POWER Automate

From no-code to low-code

Menu
  • Expressions
  • Filters
  • General
  • Problems
  • Triggers
  • Application specific solutions
    • Excel
    • Forms
    • Planner
    • Outlook
    • SharePoint
    • Teams
  • Resources
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…


Do you struggle with the various expressions, conditions, filters, or HTTP requests available in Power Automate?

I send one email per week with a summary of the new solutions, designed to help even non IT people to automate some of their repetitive tasks.

All subscribers have also access to resources like a SharePoint Filter Query cheat sheet or Date expressions cheat sheet.

Zero spam, unsubscribe anytime.

Leave a Reply Cancel reply

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

Now available:

The Ultimate Power Automate expressions cheat sheet
Equip yourself with the tool to translate your thoughts into Power Automate expressions!

Do you struggle with the various expressions, conditions, filters, or HTTP requests available in Power Automate?

I send one email per week with a summary of the new solutions, designed to help even non IT people to automate some of their repetitive tasks.

All subscribers have also access to resources like a SharePoint Filter Query cheat sheet or Date expressions cheat sheet.

Zero spam, unsubscribe anytime.

Hello and welcome!

My name is Tom and I'm a business process automation consultant and Microsoft MVP living in the Czech Republic. I’ve been working with Microsoft technologies for almost 10 years, currently using mainly Power Automate, SharePoint, Teams, and the other M365 tools.

I believe that everyone can automate part of their work with the Power Automate platform. You can achieve a lot by "clicking" the flows in the designer, but you can achieve much more if you add a bit of coding knowledge. And that's what this blog is about.

To make the step from no-code Power Automate flows to low-code flows: using basic coding knowledge to build more complex yet more efficient flows to automate more of your daily tasks.

  • Check if email already exists in Outlook contacts (Power Automate)August 7, 2022
  • Set permissions for a SharePoint list or library with Power AutomateAugust 3, 2022
  • Handle deleted files in synchronised SP library in Power AutomateJuly 31, 2022
  • Create item with lookup column in a dynamic list (Power Automate)July 27, 2022
  • How to move list between SharePoint tenants with Power AutomateJuly 24, 2022

Power Automate blogs worth visiting

Damien Bird
Dennis (Expiscornovus)
Paul Murana

© 2022 Let's POWER Automate | Powered by Superbs Personal Blog theme