Try it as below:
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('Groups')/items",
type: "GET",
headers: {
"accept": "application/json;odata=verbose",
},
success: function (data) {
var items = data.d.results;
for(var item in items){
var itemUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/getByTitle('Groups')/getItemById(" + item.ID + ")";
deleteItem(itemUrl);
}
},
error: function (error) {
console.log(JSON.stringify(error));
}
});
function deleteItem(itemUrl) {
$.ajax({
url: itemUrl,
type: "DELETE",
headers: {
"accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"If-Match": "*"
},
success: function (data) {
console.log("deleted item")
},
error: function (error) {
console.log(JSON.stringify(error));
}
});
}
Using JSOM, you can delete multiple item in batch:
function deleteListItems() {
var clientContext = SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle('TestList');
for(var i = 0; i<= 100; i++){
//assuming the IDs are available, else need some validation
var oListItem = oList.getItemById(i);
oListItem.deleteObject();
}
clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);
}
function onQuerySucceeded() {
alert('Items Deleted');
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
deleteListItems();
Reference - Batch operations using JSOM
Using CAML query, you do it as below:
var clientContext = SP.ClientContext.get_current();
var customList = clientContext.get_web().get_lists().getByTitle('Test');
var itemColl;
var camlQuery = new SP.CamlQuery();
//set your caml query here
camlQuery.set_viewXml(
'<View><Query><Where><Eq>' +
'<FieldRef Name=\'Title\'/><Value Type=\'Text\'>x</Value>' +
'</Eq></Where></Query></View>');
itemColl = customList.getItems(camlQuery);
clientContext.load(itemColl);
clientContext.executeQueryAsync(deleteitemColl, onGetitemCollFail);
function deleteitemColl() {
var itemArray = new Array();
var listItemEnumerator = itemColl.getEnumerator();
while (listItemEnumerator.moveNext()) {
var item = listItemEnumerator.get_current();
itemArray.push(item);
}
var i;
for (i = 0; i < itemArray.length; i++) {
customList.getItemById(itemArray[i].get_id()).deleteObject();
}
clientContext.executeQueryAsync(onDeleteitemCollSuccess, onDeleteitemCollFail);
}
function onGetitemCollFail(sender, args) {
console.log('Unable to get completed items. Error:' + args.get_message() + '\n' + args.get_stackTrace());
}
function onDeleteitemCollSuccess() {
console.log('Completed orientations have been deleted.');
}
function onDeleteitemCollFail(sender, args) {
console.log('Unable to delete completed items. Error:' + args.get_message() + '\n' + args.get_stackTrace());
}