For sharepoint list, use below code:
if (list.BaseType == SPBaseType.GenericList)
{
if (list != null)
{
int itemCount = list.ItemCount;
for (int k=0; k<itemCount; k++)
{
listItems[k].Recycle();
}
}
}
Or use the Batchcommand to delete all list items as below:
public static void DeleteAllItems(string site, string list)
{
using (SPSite spSite = new SPSite(site))
{
using (SPWeb spWeb = spSite.OpenWeb())
{
StringBuilder deletebuilder = BatchCommand(spWeb.Lists[list]);
spSite.RootWeb.ProcessBatchData(deletebuilder.ToString());
}
}
}
private static StringBuilder BatchCommand(SPList spList)
{
StringBuilder deletebuilder= new StringBuilder();
deletebuilder.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");
string command = "<Method><SetList Scope=\"Request\">" + spList.ID +
"</SetList><SetVar Name=\"ID\">{0}</SetVar><SetVar Name=\"owsfileref\">{1}</SetVar><SetVar Name=\"Cmd\">Delete</SetVar></Method>";
foreach (SPListItem item in spList.Items)
{
deletebuilder.Append(string.Format(command, item.ID.ToString(),item["FileRef"].ToString()));
}
deletebuilder.Append("</Batch>");
return deletebuilder;
}
reference - Deleting all the items
// We prepare a String.Format with a String.Format, this is why we have a {{0}}
string command = String.Format("<Method><SetList Scope=\"Request\">{0}</SetList><SetVar Name=\"ID\">{{0}}</SetVar><SetVar Name=\"Cmd\">Delete</SetVar><SetVar Name=\"owsfileref\">{{1}}</SetVar></Method>", list.ID);
// We get everything but we limit the result to 100 rows
SPQuery q = new SPQuery();
q.RowLimit = 100;
// While there's something left
while (list.ItemCount > 0)
{
// We get the results
SPListItemCollection coll = list.GetItems(q);
StringBuilder sbDelete = new StringBuilder();
sbDelete.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");
Guid[] ids = new Guid[coll.Count];
for (int i=0;i<coll.Count;i++)
{
SPListItem item = coll[i];
sbDelete.Append(string.Format(command, item.ID.ToString(), item.File.ServerRelativeUrl));
ids[i] = item.UniqueId;
}
sbDelete.Append("</Batch>");
// We execute it
web.ProcessBatchData(sbDelete.ToString());
list.Update();
}
Deleting all the items from a list