Delete Files From SharePoint Document Library Using Rest API in Power Automate

Sometimes, you may want to delete files from the SharePoint document library permanently. To do this, you can use Rest Api in Power Automate.

In this tutorial, I will show you how to delete a file from SharePoint using the REST API in Power Automate.

Delete Files From SharePoint Document Library Using Rest API in Power Automate

If you think we can delete the file using the Delete file action, why the Rest API? The Delete file action required the file identifier, meaning you must take another action to retrieve it. Then, it only deletes the file to the Recycle Bin and doesn’t offer advanced options like permanent deletion.

Suppose you are working on a project, and now that it’s completed, you want to delete a file or multiple files or delete files based on certain conditions. In this tutorial, I’ll explain how to do all of that.

Delete Single File From a SharePoint Library

Let’s start with the easiest example: deleting a single file in the SharePoint library using Rest API in Power Automate.

Below is a SharePoint Library set where I want to delete the Project Deadlines Excel file.

Deleting One Single File in a SharePoint Library Using Power Automate

Here are the steps to do this:

  1. Open Power Automate and create an Instant cloud flow that will trigger the flow manually.
  2. Add a Send an HTTP request to SharePoint action under the trigger and provide the below parameters:
    • Site Address: Select the SharePoint site address from the dynamic content.
    • Method: Select DELETE as the method.
    • URI: Provide the below URI:
_api/web/getFileByServerRelativeUrl('/sites/PowerAutomateTutorial/ReportStorage/Project Deadlines.xlsx')

Here:

  • PowerAutomateTutorial: Site Internal Name
  • ReportStorage: SharePoint Library Internal Name
  • Project Deadlines.xlsx: File Name
Power Automate to delete files in SharePoint
  1. Now save the flow and run it manually. After the flow successfully runs, go to the SharePoint library. You can see the file has been deleted.
Power Automate Deleting One Single File in a SharePoint Library

Note:

Test this flow with your demo file. Once you delete the file using this flow, you cannot retrieve it, as it permanently deletes the file.

Delete Multiple Files From the SharePoint Document Library

Deleting multiple files in the SharePoint library is a little different from deleting one file.

Below is a SharePoint library where I want to delete all files:

How do I Delete multiple files in Power Automate

To do this, follow the below steps:

  1. Go to Power Automate and click Create -> Instant Cloud Flow. Select Manually trigger a flow as the trigger.
  2. Then, add a Get files (properties only) action to get all the file names. Provide the below parameters:
    • Site Address: Select the site where the library is present.
    • Library Name: Select the library from the drop-down.
Power Automate Delete Files From SharePoint Document Library Using Rest API
  1. Add a Send an HTTP request to SharePoint action to delete files and provide the below parameters:
    • Site Address: Select the SharePoint site address from the dynamic content.
    • Method: Select DELETE as the method.
    • URI: Provide the below URI:
_api/web/getFileByServerRelativeUrl('/sites/PowerAutomateTutorial/ Full Path ')

Where:

  • Full Path: Replace the Full Path from Get files (properties only) using dynamic content.

It will automatically add the for each loop.

Delete Multiple Files From the SharePoint Document Library using REST API in Power Automate
  1. Now, save the flow: click Test (top-right corner), select Manually, click Test again, and click Run flow.
  2. After the flow successfully runs, go to the SharePoint library. You can see the file has been deleted.
Delete File in SharePoint using REST API in Power Automate

Delete Files From the SharePoint Document Library Based on Condition

Sometimes, you only want to delete specific files, like those older than a certain date or those with a particular name.

We will filter the files and use the REST API in Power Automate to delete only those that match our condition.

Let’s say you want to automatically delete all .pdf files older than 30 days in a SharePoint library.

Filter the Files From the SharePoint Library

  1. Create an Instant cloud flow with a trigger (Manually trigger a flow).
  2. Add the Get files (properties only) action:
    • Site Address: Select your SharePoint site.
    • Library Name: Choose your document library.
Deleting a file from a Document Library with REST API in Power Automate

This will return all files from the library.

  1. Add an Apply to each loop to review each file from the “Get files” action.
Deleting Folder File via REST API in Power Automate
  1. Inside the loop: Use the Condition control to check:
    • If file is a PDF -> Name ends with .pdf
    • And file is older than 30 days
File Name with extentation ends with '.pdf' 
AND 
Created date is less than addDays(utcNow(), -30)
Delete file from SharePoint REST API call with PowerAutomate

Note:

If you’re using the Created field from SharePoint, it’s usually available in dynamic content. Otherwise, use item()?[‘Created’] in an expression.

Delete Files From the SharePoint Library

Now the condition control will give the file which we want to delete; we need to add the last steps:

  1. Add a Send an HTTP request to SharePoint action to delete files and provide the below parameters:
    • Site Address: Select the SharePoint site address from the dynamic content.
    • Method: Select DELETE as the method.
    • URI: Provide the below URI:
_api/web/getFileByServerRelativeUrl('/sites/PowerAutomateTutorial/items()?[{'FullPath'}] ')
Delete Files From the SharePoint Document Library Based on Condition using REST API in Power Automate

This will delete the PDF file permanently if it matches both conditions.

  1. Save the flow and run it manually. Once the flow runs successfully.
  2. Go to the SharePoint document library. You will see that all PDF files created more than 30 days ago have been deleted.
Power Automate Delete Files From the SharePoint Document Library Based on Condition

On the left, you can see the files before deletion; on the right, you can see the result after the files were deleted.

These are some ways to permanently delete files from a SharePoint document library using Power Automate and REST API.

While using the Delete file action is quick, the REST API gives you more control, especially when you want to delete multiple files or delete files based on conditions permanently.

I hope you found this tutorial helpful! If you have any questions or suggestions, feel free to drop them in the comments.

Other Power Automate tutorials you may also like:

Power Apps functions free pdf

30 Power Apps Functions

This free guide walks you through the 30 most-used Power Apps functions with real business examples, exact syntax, and results you can see.

Download User registration canvas app

DOWNLOAD USER REGISTRATION POWER APPS CANVAS APP

Download a fully functional Power Apps Canvas App (with Power Automate): User Registration App