Delete all items from SharePoint online list using JavaScript object model

delete all items list sharepoint online jsom

In this post we will discuss how we can delete all items from a list using JavaScript object model in SharePoint Online Office 365. The same code will also work for SharePoint 2016 and SharePoint 2013 also.


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

Also your read:

Step by step develop workflows using visual studio 2015 as an add in and deploy to host web in SharePoint online?

Working with Content Type Hub in SharePoint 2013

The below code we are putting inside a script editor web part which we have put inside a web part page.

<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:

delete all items list sharepoint online jsom
delete all items list sharepoint online jsom


Hope this will be helpful.

Check out Best Alternative to InfoPath -> Try Now

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 →