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).
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.
- async function loadScript(url) {
- let response = await fetch(url);
- let script = await response.text();
- eval(script);
- }
-
- let scriptUrl = 'https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js'
- loadScript(scriptUrl);
The above code will load jQuery in the browser.
Now, paste the following code in the browser window and then press Enter.
- function DeleteAllListItems(list)
- {
- $.ajax({
- url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('"+list+"')/items?$top=5000",
- type: "GET",
- headers: {
- "accept": "application/json;odata=verbose",
- },
- success: function (data) {
- var items = data.d.results;
- for(var i =0 ; i< items.length;i++){
- var url = _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/getByTitle('"+list+"')/getItemById("+items[i].ID+")"
- DeleteByURL(url);
- }
- alert("Completed");
- },
- error: function (error) {
- alert(JSON.stringify(error));
- }
- });
- }
-
-
- function DeleteByURL(url)
- {
- return $.ajax({
- url: url,
- type: "POST",
- async:false,
- headers: {
- "ACCEPT": "application/json;odata=verbose",
- "content-type": "application/json;odata=verbose",
- "X-RequestDigest": $("#__REQUESTDIGEST").val(),
- "IF-MATCH": "*",
- "X-HTTP-Method": "DELETE"
- },
- });
- }
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.
- DeleteAllListItems(“CustomerData”);
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.