How to delete all items from SharePoint online list using Rest API?

delete all items from sharepoint online list using rest api
Check out Best Alternative to InfoPath


Here we will discuss how 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 all items from sharepoint online list using rest api
delete all items from sharepoint online 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.

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.

Also, you can read a few useful articles below:
How to connect to SharePoint online site using SharePoint Online Management Shell?

Steps to create a provider-hosted add-in in SharePoint Online and host in Microsoft Azure using Microsoft Azure Web Apps method

How to deploy WebJob to Microsoft Azure using Upload zip file method in SharePoint Online?

The full code looks like below:

<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 Announcements list.


Hope this will be helpful.


You May Also like the Following SharePoint Online Tutorials:

About 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

View all posts by Bijay Kumar →