Aidan Garnish

Collaboration Not Competition

Delete all items in a SharePoint list more efficiently

One way to remove all items from a SharePoint list is to iterate through every item and call delete like this.

However, a more efficient way to clear down an entire list is to use the ProcessBatchData method like this:

private void deleteAllListItems(SPSite site, SPList list)

  StringBuilder sbDelete = new StringBuilder();

  sbDelete.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");


  string command = "<Method><SetList Scope=\"Request\">" + list.ID + "</SetList><SetVar Name=\"ID\">{0}</SetVar><SetVar     Name=\"Cmd\">Delete</SetVar></Method>";

  foreach (SPListItem item in list.Items)
  {
    sbDelete.Append(string.Format(command, item.ID.ToString()));
  }

  sbDelete.Append("</Batch>");

  site.RootWeb.ProcessBatchData(sbDelete.ToString());
}

Pingbacks and trackbacks (1)+

Add comment

Loading