Delete all items from SharePoint list programmatically using JSOM

In this SharePoint tutorial, we will discuss how to delete all items from the SharePoint list programmatically javascript object model (jsom) in SharePoint Online.


In SharePoint Online we can only use client-side object model code, we can not use any server-side code.

In this particular example, we got a requirement to delete all items from a SharePoint online list using the JavaScript object model (jsom).


Here we are using a SharePoint online site Office 365 but the same code will also work for SharePoint 2016 and SharePoint 2013.


In this jsom example I have taken a button and on click of that button it will delete all items from a list known as “SourceList”. And I have added the jsom code inside a script editor web part which we have put inside a web part page in SharePoint Online.


If you are new to JSOM (JavaScript Object Model), then you can download a FREE PDF Of 51 JSOM Examples in SharePoint Online.


Here in the CAML query, we are passing the RowLimit as 100, you can provide as per the requirement.


Delete all items from sharepoint list programmatically javascript client object model (jsom)


Below is the full JavaScript Object model code to delete all items from the sharepoint list programmatically.


<input type="button" id="btnSubmit" value="Delete All Items" /><br/>
<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 () {
deleteAllItemsFromList();
});
}
var clientContext;
var website;
var oList;
var cnt = 0;
function deleteAllItemsFromList() {
clientContext = SP.ClientContext.get_current();
website = clientContext.get_web();
oList = website.get_lists().getByTitle('SourceList');
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><RowLimit>100</RowLimit></View>');
this.collListItem = oList.getItems(camlQuery);
clientContext.load(website);
clientContext.load(collListItem, 'Include(Id)');
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded(sender, args) {
var listItemInfo = '';
var listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
var ID = oListItem.get_id();
var oListItemDel = oList.getItemById(ID);
oListItemDel.deleteObject();
clientContext.executeQueryAsync(Function.createDelegate(this, this.onDeleteSucceeded), Function.createDelegate(this, this.onDeleteFailed));
}
}
function onQueryFailed(sender, args) {
alert('Failed');
}
function onDeleteFailed(sender, args) {
alert('Failed');
}
function onDeleteSucceeded(sender, args) {
cnt = cnt + 1;
alert('Delete success : ' + cnt);
}
</script>


Once you will save the code and click on the button to delete, it will display an alert on each item delete like below:

SharePoint Online Delete all items from list using jsom


I hope this will be helpful to you to delete all items from sharepoint list programmatically javascript or jsom in SharePoint Online or SharePoint 2016/2013.


Source: SP Guide

Recent Posts

See All