Delete List Item In SharePoint Using REST API

In this blog, I would like to share the code to delete the SharePoint list item using SharePoint Rest API.

Here is the detailed documentation for the lists and their features.

Steps to delete the SharePoint list Item

Follow the below steps to delete an item.

Step 1

Create one JS file or you can use the Content Editor Web Part.

Step 2

Below is the API to get the particular list item using Item ID, you can browse this URL in the browser to check whether the API is working or not.

https://sharepoint.com/_api/web/lists/GetByTitle('samplelist')/items/getbyid(1)

Note

Replace your list name with “samplelist”.

Step 3

Refer the jQuery in your HTML file or CEWP.

Use the below code to delete the item from a SharePoint list.

  1. 'use strict';  
  2. ExecuteOrDelayUntilScriptLoaded(initializePage, "sp.js");  
  3.   
  4. function initializePage() {  
  5.     var siteURL;  
  6.     var itemsArray = [];  
  7.     // This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model  
  8.     $(document).ready(function() {  
  9.         var scriptbase = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/";  
  10.         DeleteListItem();  
  11.     });  
  12.     //Retrieve list items from sharepoint using API  
  13.     function DeleteListItem() {  
  14.         siteURL = _spPageContextInfo.webAbsoluteUrl;  
  15.         console.log("from top nav - " + siteURL);  
  16.         var apiPath = siteURL + "/_api/lists/getbytitle(''samplelist'')/items/getbyid(1)";  
  17.         $.ajax({  
  18.             url: apiPath,  
  19.             type: "DELETE",  
  20.             headers: {  
  21.                 Accept: "application/json;odata=verbose"  
  22.             },  
  23.             async: false,  
  24.             success: function(data) {  
  25.                 alert("Item Deleted successfully");  
  26.             },  
  27.             eror: function(data) {  
  28.                 console.log("An error occurred. Please try again.");  
  29.             }  
  30.         });  
  31.     }  
  32. }  

Summary

In this blog, we have explored how to delete a particular item from SharePoint list using REST API.