views:

27

answers:

1

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

A: 

Hi,

I don't think is possible to bypass the recycle bin using batch update however you could modify you code to something like this

foreach (SPListItem item in itemCollection)
   {
      item.Delete(); //this wont go to the recycle bin
      //or if you need to send it to the recycle bin then you can use
      item.Recycle();


   }
Renzo
Thank for your comment,But, your code will got a big problem when user wanna delete a bulk of data. It will take long time to do, so that is bad performance.
q-tuyen
Hi yes I know however you are already instantiating every item to get the id and the parent list?Another thing that might work for you is to run the code elevated so that the items wont be on the end users Recycle bin but on the system's account one. Just thoughts :)
Renzo