Delete All items from the SharePoint List using PnP core CSOM Library Programmatically

Welcome to our new article on “Delete All Items from the SharePoint List using PnP core CSOM Library programmatically”. In this SharePoint PnP article, we will learn about how to delete all items from the list of SharePoint online using PnP. The same code also will work for SharePoint 2013/2016.

If you are new to PnP SharePoint, follow this article on PnP SharePoint Online SharePoint Online Development using Patterns and Practices (PnP) and Rename SharePoint List Title Column Programmatically using PnP CSOM

Delete All Items from the SharePoint List using PnP Core CSOM Library

In my SharePoint Online site, I have a list name called “Order Details” with few items. We will see how we can delete all items from this SharePoint Online list programmatically using PnP core CSOM library code.

Delete all items from the SharePoint list programmatically using PnP core CSOM Library
Delete all items from the SharePoint list programmatically using PnP core CSOM Library

The below references we have to use in the code to work with PnP libraries:

  • Microsoft.SharePoint.Client
  • OfficeDevPnP.Core

Here I have created an asp.net application and I have added a button. Just click on the button, it will Delete all items from the list of SharePoint Online using the PnP core CSOM library.

<div>
<asp:Button ID="btnDeleteItems" runat="server" Text="Delete Items From the List" OnClick="btnDeleteItems_Click" /><br />
<asp:Label ID="lblDeleteItems" runat="server" Text=""></asp:Label>
</div>

Below given snippet code is delete all items from the list of SharePoint online using PnP.

AuthenticationManager authMgr = new AuthenticationManager();
string siteURL = https://DomainName.sharepoint.com/sites/TSInfoPNP";
string userName = "*******@DomainName.onmicrosoft.com";
string password = "******";
protected void btnDeleteItems_Click(object sender, EventArgs e)
{
deleteItemsInList();
}
void deleteItemsInList()
{
try
{
using (var ctx = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteURL, userName, password))
{
List oList = ctx.Web.GetListByTitle("Order Details");
ctx.ExecuteQueryRetry();
CamlQuery query = CamlQuery.CreateAllItemsQuery(4000);					Microsoft.SharePoint.Client.ListItemCollection items = oList.GetItems(query);
ctx.Load(items);
ctx.ExecuteQuery();
for (int i = items.Count - 1; i >= 0; i--)
{
items[i].DeleteObject();
}
ctx.ExecuteQuery();
lblDeleteItems.Text = "All Items Deleted from the List Successfully";
}
}
catch (Exception ex)
{
}
}

		

Run your application and click on button on the page.

Delete all items from the SharePoint list programmatically using PnP core CSOM Library
Delete all items from the SharePoint list programmatically using PnP core CSOM Library

Go and check your SharePoint Online list all the items are deleted as shown below.

Delete all items from the SharePoint list programmatically using PnP core CSOM Library
Delete all items from the SharePoint list programmatically using PnP core CSOM Library

You may like following SharePoint list and library tutorials:

In this SharePoint PnP article, we learned about how to delete all items programmatically from list of SharePoint online using PnP core CSOM Library.

>