Deleting list items via ProcessBatchData()
- by q-tuyen
You can build a batch string to delete all of the items from a SharePoint list like this:
1: //create new StringBuilder
2: StringBuilder batchString= new StringBuilder();
3:
4: //add the main text to the stringbuilder
5: batchString.Append("");
6:
7: //add each item to the batch string and give it a command Delete
8: foreach (SPListItem item in itemCollection)
9: {
10: //create a new method section
11: batchString.Append("");
12: //insert the listid to know where to delete from
13: batchString.Append("" + Convert.ToString(item.ParentList.ID) + "");
14: //the item to delete
15: batchString.Append("" + Convert.ToString(item.ID) + "");
16: //set the action you would like to preform
17: batchString.Append("Delete");
18: //close the method section
19: batchString.Append("");
20: }
21:
22: //close the batch section
23: batchString.Append("");
24:
25: //preform the batch
26: SPContext.Current.Web.ProcessBatchData(batchString.ToString());
The only disadvantage that I can think of right know is that all the items you delete will be put in the recycle bin. How can I prevent that?
I also found the solution like below:
// web is a the SPWeb object your lists belong to
// Before starting your deletion
web.Site.WebApplication.RecycleBinEnabled = false;
// When your are finished re-enable it
web.Site.WebApplication.RecycleBinEnabled = true;
Ref [Here](http://www.entwicklungsgedanken.de/2008/04/02/how-to-speed-up-the-deletion-of-large-amounts-of-list-items-within-sharepoint/)
But the disadvantage of that solution is that only future deletion will not be sent to the Recycle Bins but it will delete all existing items as well which user do not want. Any idea to prevent not to delete existing items?
Many thanks in advance,
TQT