Delete all items from List using JavaScript Object Model (jsom) in SharePoint Online/2013/2016

This jsom SharePoint tutorial explains, how to delete all items from the list using the JavaScript object model (jsom) in SharePoint Online/2013/2016. We will also see, how to delete a list item by id using the javascript object model (jsom) in SharePoint Online or SharePoint 2013/2016.

We will use jsom code to delete all items from SharePoint list using JavaScript.

Delete all Items from list using JavaScript Object model (jsom) in SharePoint Online

Here we will see, how to delete all SharePoint list items using the JavaScript object model (jsom) in SharePoint Online Office 365. I have used to code inside a script editor web part inside a web part page in SharePoint.

<input type="button" id="btnDeleteItems" value="Click Here"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

<script>
$(document).ready(function(){
$("#btnDeleteItems").click(function(){
DeleteAllItems();
});
});

function DeleteAllItems(){
var ctx = SP.ClientContext.get_current(),
list = ctx.get_web().get_lists().getByTitle('Ideas'),
query = new SP.CamlQuery(),
items = list.getItems(query);
ctx.load(items, "Include(Id)");
ctx.executeQueryAsync(function () {
var enumerator = items.getEnumerator(),
itemArray = [];
while (enumerator.moveNext()) {
itemArray.push(enumerator.get_current());
}

for (var s in itemArray) {
itemArray[s].deleteObject();
}
ctx.executeQueryAsync();
});
}
</script>

This will delete all items from the SharePoint Online list using jsom (JavaScript object model).

Delete all items from SharePoint online list using JavaScript object model

This is another way we can delete all items from a list using the JavaScript object model (jsom) in SharePoint Online Office 365. The same code we can use to delete all items from SharePoint 2016 and SharePoint 2013 list.

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

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 from sharepoint list javascript
Delete all items from List using JavaScript Object Model (jsom) in SharePoint Online

You may like following SharePoint jsom tutorials:

Hope this SharePoint jsom tutorial explains, how to delete all items from SharePoint list using JavaScript object model (jsom) in SharePoint Online Office 365. The same way we can delete all items from sharepoint list using jsom in SharePoint 2016 and SharePoint 2013.

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

  • […] Delete all items from List using JavaScript Object Model (jsom) in SharePoint Online/2013/2016 […]

  • […] The above code will delete a single item, but if you want to delete all items from a SharePoint list, then you can read a tutorial on how to delete all items from a SharePoint list using jsom? […]

  • >