Delete SharePoint List Items In Browser Console

This article demonstrates how to delete SharePoint list items in browser console using REST API without writing any code in the SharePoint site. To delete list items just copy paste the code in the console window of the browser and then call the function by passing the List name whose items have to be deleted.

Introduction

This article demonstrates how to delete SharePoint list items in browser console using REST API without writing any code on the SharePoint site. To delete list items, just copy paste the code in the console window of the browser and then call the function by passing the List name whose items have to be deleted. The code will delete the items one by one until it is empty.

Accessing the console and pasting the code in the console window of the browser

To delete the items of a Sharepoint List, open the SharePoint site in the browser and press  F12.

This will open the Developers Tool Pane on the screen, as shown below (For Chrome browser).

SharePoint

Click on Console option in the developer screen window and then copy paste the following code in the console window and press the Enter Key.

  1. async function loadScript(url) {  
  2.   let response = await fetch(url);  
  3.   let script = await response.text();  
  4.   eval(script);  
  5. }  
  6.   
  7. let scriptUrl = 'https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js'  
  8. loadScript(scriptUrl);  

SharePoint
The above code will load jQuery in the browser. 

Now, paste the following code in the browser window and then press Enter.

  1. function DeleteAllListItems(list)  
  2. {  
  3. $.ajax({  
  4.     url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('"+list+"')/items?$top=5000",  
  5.     type: "GET",  
  6.     headers: {  
  7.         "accept""application/json;odata=verbose",  
  8.     },  
  9.     success: function (data) {  
  10.         var items = data.d.results;  
  11.         for(var i =0 ; i< items.length;i++){  
  12.             var url = _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/getByTitle('"+list+"')/getItemById("+items[i].ID+")"  
  13.             DeleteByURL(url);  
  14.         }  
  15.         alert("Completed");  
  16.     },  
  17.     error: function (error) {  
  18.         alert(JSON.stringify(error));  
  19.     }  
  20. });  
  21. }  
  22.   
  23.   
  24. function DeleteByURL(url)  
  25.  {  
  26.  return $.ajax({  
  27.    url: url,  
  28.    type: "POST",  
  29.     async:false,  
  30.    headers: {  
  31.     "ACCEPT""application/json;odata=verbose",  
  32.     "content-type""application/json;odata=verbose",  
  33.     "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
  34.     "IF-MATCH""*",  
  35.     "X-HTTP-Method""DELETE"  
  36.    },  
  37.   });  
  38.  }  

SharePoint
This will define two functions, i.e., DeleteAllListItems and DeleteByURL which is internally called by the DeleteAllListItems function.

Now, to delete the list items, simply call the DeleteAllListItems function and pass the list name as a parameter to the function.

For example: if the name of the list is CustomerData, simply paste the following code in the console of the browser and press Enter key.

  1. DeleteAllListItems(“CustomerData”);  

 

SharePoint
If you view the list in the all Site Content page, you will see that the list item count is decreasing and it will show that the list was modified one minute ago. 

Summary

In this article, we discussed how we can delete items stored in a SharePoint list just by pasting some code in the console of the browser. This will help the users to delete items in a list which have a very large amount of items as SharePoint does not give any option to delete all the items from a list with a single click.