Delete all items from SharePoint Online list using Rest API

This SharePoint Rest API tutorial explains, how to delete all items from a SharePoint Online list using Rest API. Also, we will see how to delete list item based on item ID using Rest API in SharePoint Online or SharePoint 2013/2016.

If you are new to SharePoint Rest API, read an article SharePoint Rest API Tutorial and Examples.

SharePoint rest api delete all list items

We can delete all items from SharePoint online list using Rest API. I have a list which has few items inside the list in my SharePoint online site. The list looks like below:

delete item from sharepoint list using rest api
delete item from sharepoint list using rest api

Below is the code which we can add into a script editor web part which we can add in a web part page in SharePoint Online or SharePoint 2013/2016.

Here is the first function we are getting the items and then we are building the rest API URL for deleting an item and passing it to the deleteItem(URL) method.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

<input type="button" id="btnDeleteAllItems" value="Delete All Items" />

<script>
$(function () {
bindButtonClick();
});
function bindButtonClick() {
$("#btnDeleteAllItems").on("click", function () {
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle(‘Announcements’)/items",
type: "GET",
headers: {
"accept": "application/json;odata=verbose",
},
success: function (data) {
var items = data.d.results;
$.each(data.d.results, function (key, value) {
var id=value.ID;
var url = "/_api/Web/Lists/getByTitle(‘Announcements’)/getItemById("+id+")";
deleteItem(url);
});
},
error: function (error) {
alert(JSON.stringify(error));
}
});
});
}

function deleteItem(url) {
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + url,
type: "DELETE",
headers: {
"accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"If-Match": "*"
},
success: function (data) {
},
error: function (error) {
alert(JSON.stringify(error));
}
});
}
</script>

Once you save the page and click on the button, it will delete all items from the SharePoint Announcements list.

SharePoint rest api delete list item by id

Now we will see how we can delete items from SharePoint list using Rest API in SharePoint Online Office 365. Here we will delete list item based on list item id using Rest API in SharePoint.

Here let us take a textbox where the user can put the item id in the textbox. On submit button, the item will get deleted from the list.

All the HTML code and Rest API code we will put inside a script editor web part inside a SharePoint web part page.

Here we have a list name as MyCompanyInfo which has one item and whose ID is 2.

sharepoint rest api delete list item by id
sharepoint rest api delete list item by id
<div>
Enter ID to Delete:
<input type="text" id="txtId" />
</div>
<div>
<input id="btnSubmit" type="button" value="Submit" />
</div>
<div id="divResult"></div>

Rest API Code:
Here we have can retrieve the item id from the textbox.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

<script>
$(function () {
bindButtonClick();
});

function bindButtonClick() {
$("#btnSubmit").on("click", function () {
deleteListItem();
});
}

function deleteListItem() {
var id = $("#txtId").val();
var siteUrl = _spPageContextInfo.webAbsoluteUrl;
var fullUrl = siteUrl + "/_api/web/lists/GetByTitle('MyCompanyInfo')/items(" + id + ")";
$.ajax({
url: fullUrl,
type: "POST",
headers: {
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"X-HTTP-Method": "DELETE",
"IF-MATCH": "*"
},
success: onQuerySucceeded,
error: onQueryFailed
});
}

function onQuerySucceeded(sender, args) {
$("#divResult").html("Item successfully deleted!");
}

function onQueryFailed() {
alert('Error!');
}
</script>

Once you Save the page, it will display like below. Once user put the item id and click on Submit button it will delete the list item and it will display the successful message.

sharepoint rest api delete all list items
sharepoint rest api delete all list items

Now if you will check the list, you will not find the item inside the list like below:

sharepoint rest api delete list item by id
sharepoint rest api delete list item by id

This is how we can delete list item by id using Rest API in SharePoint Online Office 365.

You may also like following Rest API SharePoint tutorials:

Hope this tutorial helps to learn how to delete all items from list using Rest API in SharePoint Online/2013/2016 as well as how to delete list item by id using Rest API in SharePoint Online or SharePoint 2013/2016.

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

>