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 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:
You may like following SharePoint jsom tutorials:
- Get Current User Details Using JavaScript Object Model (jsom) and jQuery in SharePoint 2013/2016/Online
- Upload multiple attachments to list items using JSOM and REST API in SharePoint Online/2013/2016
- Insert item to SharePoint list using JavaScript Object Model JSOM in SharePoint Online
- Read, create, update, delete a file using JavaScript object model (jsom) in SharePoint
- SharePoint rest api create a folder
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.
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
[…] 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? […]