Showing posts with label Batch Delete. Show all posts
Showing posts with label Batch Delete. Show all posts

March 27, 2025

Bulk Delete SharePoint List Items Using Power Automate and REST API

Introduction

Deleting items one by one from a SharePoint list can be time-consuming and inefficient, especially when dealing with large volumes of data. Using Power Automate in combination with SharePoint’s $batch REST API offers a much faster and more scalable solution. In this article, we’ll walk through how to build a Flow that can delete multiple SharePoint list items in a single batch request - ideal for cleanups, data resets, or bulk removals.

Why Use Batch Operations?

  • Performance: Fewer API calls improve speed and reduce throttling risks.
  • Scalability: Handles thousands of items effortlessly.
  • Automation: No more manual deletions.

Batch Delete SharePoint List Items - Use Case

You’re managing a SharePoint list, and need to delete all existing items to start fresh. Let’s build the Flow step-by-step. Manually deleting items or making individual API calls for a large SharePoint list is slow and tedious. With Power Automate, you can batch delete items efficiently. Here’s how.

Batch Delete Flow

Setting Up the Batch Delete Flow

The goal is to delete all items in batches, looping until the list is empty.

1. Initialize Variables

Add a Set Variable action:

  • Name: ItemCount 
  • Type: Integer 
  • Value: -1 (to initiate the loop) 

This variable track remaining items and drives the deletion loop. 

2. Add Scope Action for Perform Batch Delete Functionality

1. Add Compose:  

Compose for Defines the SharePoint list details, including the site address and list name. 

{
     "siteAddress": "https://Tenant-Name.sharepoint.com/sites/Site-Name",
     "listName": "List Name"
}

Replace the placeholders with your tenant, site, and list names. 

2.  Add Compose for Batch Delete Template: -  

Prepares a template for batch deletion, specifying the structure of requests to delete multiple items. The Batch Delete Template in Power Automate allows us to delete multiple items from a SharePoint list in a single request. This is done using the _api/web/lists/getByTitle endpoint, where items are deleted based on their IDs. 

--changeset_@{actions('settings_Sharepoint_List')?['trackedProperties']['changeSetGUID']}
Content-Type: application/http
Content-Transfer-Encoding: binary

DELETE @{outputs('settings_Sharepoint_List')['siteAddress']}/_api/web/lists/getByTitle('@{outputs('settings_Sharepoint_List')['listName']}')/items(|ID|)
HTTP/1.1
Content-Type: application/json;odata=verbose
Accept: application/json;odata=verbose
IF-MATCH: *

Note on Formatting: Ensure that you maintain the empty line between the headers (ending with IF-MATCH: *) and the DELETE command. The SharePoint API requires this specific whitespace to identify where the headers end and the operation begins.

Step 2: Looping Through the Deletion Process 

Add a Do Until loop that runs until ItemCount equals 0. Inside the loop: 

1. Get SharePoint List Items 

Add a Get Items action: 

  • Site Address: @{outputs('settings_Sharepoint_List')['siteAddress']} 
  • List Name: @{outputs('settings_Sharepoint_List')['listName']} 
  • Top Count: 4999 (SharePoint’s retrieval limit). 

2. Update ItemCount 

Add a Set Variable action: 

@{length(body('Get_items')['value'])}

3. Select Item IDs 

Add a Select action: 

From: @{body('Get_items')['value']}
Map: "@replace(outputs('BatchDelete_Template'), '|ID|', string(item()['Id']))"

4. Combine Requests 

Add a Compose action (named BatchDelete): 

@{join(body('Select_Item_ID'), decodeUriComponent('%0A'))}

5. Send the Batch Delete Request 

Send an HTTP Request to SharePoint

Add a Send an HTTP Request to SharePoint action: 

Method: POST 
URI:
/_api/$batch
Headers:
X-RequestDigest: digest
Content-Type: multipart/mixed; boundary=batch_@{actions('settings_Sharepoint_List')?['trackedProperties']['batchGUID']}
Body:
--batch_@{actions('settings_Sharepoint_List')?['trackedProperties']['batchGUID']}
Content-Type: multipart/mixed; boundary="changeset_@{actions('settings_Sharepoint_List')?['trackedProperties']['changeSetGUID']}"

@{outputs('BatchDelete')}
--changeset_@{actions('settings_Sharepoint_List')?['trackedProperties']['changeSetGUID']}--
--batch_@{actions('settings_Sharepoint_List')?['trackedProperties']['batchGUID']}--

CRITICAL: When pasting the body into the "Send an HTTP Request to SharePoint" action, you must include the blank lines before the @{outputs('BatchDelete')} expression. These line break act as delimiters for the batch request; omitting them will result in a "Bad Request" error. 

6. Check Results 

Add a Compose action: 

 @{base64ToString(body('Send_an_HTTP_request_to_SharePoint')['$content'])}

How It Works?

  • The loop fetches up to 4999 items per batch, prepares a batch DELETE request, and repeats until ItemCount is 0.
  • Once complete, your list is empty!

Why Choose Batch Deletion?

  • Speeds Things Up: Cuts down API calls for a smoother run.
  • Tackles Big Loads: Clears up to 4999 items in one go.
  • Ditches the Grind: Turns a chore into a hands-off win.

Conclusion

Using Power Automate with the SharePoint REST API offers a practical and efficient way to delete list items in bulk. Whether you're performing a routine cleanup or resetting data, this approach helps streamline the process and save time.

If you have any questions you can reach out our SharePoint Consulting team here.