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 that has few items inside the list in my SharePoint online site. The list looks like below:
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.
<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.
Now if you will check the list, you will not find the item inside the list like below:
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:
- Create, Update and Delete SharePoint List using Rest API in SharePoint 2013/2016/Online
- Retrieve list data using AngularJS REST API in SharePoint 2013
- Get Current Logged in User and Display Name using SharePoint 2013 REST API
- Create and Update SharePoint 2013 group using Rest API
- Rest API Create and delete folder inside document library in SharePoint 2013
- Filter Records based on conditions using REST API in SharePoint 2013
- SharePoint 2013 list item level permission using REST API
- Various ways to add item into a SharePoint 2013 List
- Get All Attachments From SharePoint 2013 List Item using Rest API
- Insert and Retrieve items to SharePoint Online list using Rest API
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.
Bhawana Rathore is a Microsoft MVP (3 times in Office Apps & Services) and a passionate SharePoint Consultant, having around 10 years of IT experience in the industry, as well as in .Net technologies. She likes to share her technical expertise in EnjoySharePoint.com and SPGuides.com
what if i have items more thatn 5k