Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete All List Items Sharepoint 2013 REST

I need to delete all the Items in a Sharepoint List using REST API.
How can I achieve this?
I can delete a single Item using: "/_api/web/lists/getByTitle('MyList')/items('ID')"

I tried to remove the ID but it did not work.

like image 826
Nk SP Avatar asked Mar 19 '14 15:03

Nk SP


People also ask

How do I delete all lists in SharePoint 2013?

10 Answers. Show activity on this post. if you have list with more than 500 items, and you don't want to chose coding path, and don't want to delete list as well, than you go to "Manage Content and Structure" view, it allows you to view 1000 items at once and than select all delete all.

How do I remove all items from a SharePoint list?

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.


2 Answers

You can try this

function deleteItem(url) {
$.ajax({
    url: _spPageContextInfo.webAbsoluteUrl + url,
    type: "DELETE",
    headers: {
        "accept": "application/json;odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val(),
        "If-Match": "*"
    },
    success: function (data) {

    },
    error: function (error) {
        alert(JSON.stringify(error));
    }
});
}

$.ajax({
    url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('MyList')/items",
    type: "GET",
    headers: {
        "accept": "application/json;odata=verbose",
    },
    success: function (data) {
        var items = data.d.results;
        for(var item in items){
            var url = "/_api/Web/Lists/getByTitle('MyList')/getItemById(item.ID)"
            deleteItem(url);
        }
    },
    error: function (error) {
        alert(JSON.stringify(error));
    }
});
like image 128
Tarek Salah uddin Mahmud Avatar answered Sep 21 '22 02:09

Tarek Salah uddin Mahmud


You have to make one delete call for each item in the list, using a URI like you showed above, passing in each ID in succession. If there are LOTS of items in the list, it would likely be cheaper and faster to delete then recreate the list itself.

like image 38
TimTheEnchanter Avatar answered Sep 23 '22 02:09

TimTheEnchanter