Hello SharePointers,
In our day to day development life we need to perform many CRUD(Create, Retrieve, Update, and Delete) operations on SharePoint List. Today, in this article we will see how to delete all items from the SharePoint list with CSOM.

CSOM, the Client Side Object Model is introduced in SharePoint 2013 from then onwards it has been very useful to every version of SharePoint like 2013/2016/2019 and SharePoint Online.

So let us move to the our main topic.

Requirement

Delete all Items from SharePoint List

Solution

In order to delete all items from SharePoint list using Client Side Object Model (CSOM), we would need to implement batch delete/paging using the ListItemCollection Property.

This method will avoid the exceptions to run for a large list or size of the query/response.

I went through some MSDN articles which suggest deleting 2000 items in a single run. But I faced an exception for below 2000 records. Maybe it is possible, but I gave up as I was facing a request time out exception.

So here what I am doing is just getting block of 4000 items and deleting them into batch of 100. Repeating the same until ListItemCollection property is null.

Also, it is important to keep in mind that items which are deleted programmatically they will not be moved in Recycle Bin of the site or Site Collection.

Below is the code block to Delete all items from SharePoint link using CSOM.

private void deleteAllFromList(ClientContext cc, List myList)
{
    int queryLimit = 4000;
    int batchLimit = 100;
    bool moreItems = true;

    string viewXml = string.Format(@"
        <View>
            <Query><Where></Where></Query>
            <ViewFields>
                <FieldRef Name='ID' />
            </ViewFields>
            <RowLimit>{0}</RowLimit>
        </View>", queryLimit);
    var camlQuery = new CamlQuery();
    camlQuery.ViewXml = viewXml;

    while (moreItems)
    {
        ListItemCollection listItems = myList.GetItems(camlQuery); // CamlQuery.CreateAllItemsQuery());
        cc.Load(listItems,
            eachItem => eachItem.Include(
                item => item,
                item => item["ID"]));
        cc.ExecuteQuery();

        var totalListItems = listItems.Count;
        if (totalListItems > 0)
        {
            Console.WriteLine("Deleting {0} items from {1}...", totalListItems, myList.Title);
            for (var i = totalListItems - 1; i > -1; i--)
            {
                listItems[i].DeleteObject();
                if (i % batchLimit == 0)
                    cc.ExecuteQuery();
            }
            cc.ExecuteQuery();
        }
        else
        {
            moreItems = false;
        }
    }
    Console.WriteLine("Deletion complete.");
}

By this way we can delete List all list items from SharePoint Online using CSOM. Hope you guys liked it. If yes then Please comment below and Share this article with collogues.

You might be interested in the below article. Please check

2 thoughts on “Delete all items from SharePoint list with CSOM”

Leave a Reply