Deleting a large number of items from a list in SharePoint

Search

Accessible SharePoint WebSites
Download ARF

Deleting a large number of items from a list in SharePoint

http://blog.thekid.me.uk

Recently the question was asked in the newsgroups about deleting a large number of items from SharePoint (WSS) in the fastest way. I had, in one if my projects, needed to remove a large number of item from SharePoint and the best way I found was to use 'ProcessBatchData' as it avoided the API and was considerably faster.

Here is some example code which will remove items from a SharePoint list. If you do this I would remember about the Recycle Bin and the effect deleting many items will have in the future...it maybe worth adding some code which also removes the items from the recycle bin once it has finished.

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

foreach (SPListItem item in CurrentList.Items)
{
    sbDelete.Append("<Method>");
    sbDelete.Append("<SetList Scope=\"Request\">" + CurrentList.ID + "</SetList>");
    sbDelete.Append("<SetVar Name=\"ID\">" + Convert.ToString(item.ID) + "</SetVar>");
    sbDelete.Append("<SetVar Name=\"Cmd\">Delete</SetVar>");
    sbDelete.Append("</Method>");
}

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

try
{
    SPContext.Current.Site.RootWeb.ProcessBatchData(sbDelete.ToString());
}
catch (Exception ex)
{
    
Console.WriteLine("Delete failed: " + ex.Message);
   
throw;
}

I took me some time to re-find this code and so I have posted it here more for my benefit, but if anyone finds it useful then great.

Posted by Vincent Rothwell on Saturday, 24 Feb 2007 15:21  - 13 Comments
Orininally printed from http://thekid.me.uk - Copyright Vincent Rothwell 2007
 

Comments

Sunday, 27 Jul 2008 10:36 by Tudor Olariu
Your code would run significantly faster if you would take CurrentList.Items out of the for declaration, and just use a variable: SPListItemCollection ItemCollection = CurrentList.Items; foreach (SPListItem item in ItemCollection) ... This, CurrentList.Items will not be evaluated at every iteration and it would save you a lot of time. Tudor

Sunday, 27 Jul 2008 10:36 by Vince
Tudor, Actually thats not quite true in this situation. The CurrentList.Items will generally create a new SPListItemCollection, but not in a foreach loop. The compiled loop gets a IEnumerator and uses that object(interface) to loop through the collection. This never goes back to the property and so never re-creates the collection. However it would be best to use your code if you were accessing the collection more than once outside of a loop. --Vince

Sunday, 27 Jul 2008 10:36 by Ricardo Henriques
Good example, thank you. Just a minor detail: I think there's a memory leak in your use of RootWeb, you should Dispose of the SPWeb object returned by RootWeb. - Ricardo

Tuesday, 20 Jan 2009 07:56 by Sasa
Ricardo, don't think you'll need to dispode SPWeb object when it comes from SPContext

Monday, 23 Feb 2009 11:30 by Mark Stokes
I am running your code Vince, but the items don't appear to be deleting. I don't get any error messages and the string returned by running the batch command is just <results></results> Is anything missing from the code?

Monday, 16 Mar 2009 06:56 by Maik van der Gaag
Hi Vincent, Great post it works extremly well. I also dedicated a blog post on my blog about it http://www.blogaboutsharepoint.com. if you don't want that the items are inserted in you recyclebin just put the following code in front off the bulk delete action. CurrentList.ParenWeb.Site.WebApplication.RecyclebinEnabled = false; and after the delettion just set it to true again. You can also read about it on my blog : http://www.blogaboutsharepoint.com/2009/03/16/bulk-deletion-of-splistitems-splistitemcollection/

Saturday, 20 Jun 2009 12:29 by sohbet
Thank you so much.

Friday, 4 Sep 2009 09:47 by Glynn
I'm terribly new at this, but is this something can be run as a script in a web part? Can in only be run from a .net client application? I have an Access2007 client program with links to Sharepoint lists and it takes FOREVER to delete items in a list. Or is this something that can be used from VBA? Thanks for your understanding in advance, Glynn

Monday, 5 Oct 2009 11:46 by sara
Try this too, <a href="http://sarangasl.blogspot.com/2009/10/delete-item-from-sharepoint-list.html"><b>Delete item from SharePoint List and more</b></a>

Thursday, 21 Jan 2010 01:21 by Chris
I am using this method (including disabling the recycle bin while it runs) to delete all documents from a document library, but it still seems painfully slow to me. A document library with 5,300 documents and 2.7 GB took roughly 3 hrs & 40 min to delete all documents. I'm running SQL Server on a Xeon CPU 3.06 GHz, 4.75 GB RAM and SAN storage. Do you know if this performance is in the expected range for this delete operation? Or, if not, do you have any ideas what might be causing the bottleneck?

Thursday, 21 Jan 2010 01:57 by Chris
I am using this method (including disabling the recycle bin while it runs) to delete all documents from a document library, but it still seems painfully slow to me. A document library with 5,300 documents and 2.7 GB took roughly 3 hrs & 40 min to delete all documents. I'm running SQL Server on a Xeon CPU 3.06 GHz, 4.75 GB RAM and SAN storage. Do you know if this performance is in the expected range for this delete operation? Or, if not, do you have any ideas what might be causing the bottleneck?

Thursday, 21 Jan 2010 02:40 by Chris
I am using this method (including disabling the recycle bin while it runs) to delete all documents from a document library, but it still seems painfully slow to me. A document library with 5,300 documents and 2.7 GB took roughly 3 hrs & 40 min to delete all documents. I'm running SQL Server on a Xeon CPU 3.06 GHz, 4.75 GB RAM and SAN storage. Do you know if this performance is in the expected range for this delete operation? Or, if not, do you have any ideas what might be causing the bottleneck?

Wednesday, 9 Jun 2010 12:33 by Govind
Hi I have 80000 infopath forms in form library with versioning enabled. Aprrox. each item has 3 to 4 version. I want to delete one year old form items from form library. Can the above code works for form library. If yes, then i tried to use it. But it result in error. Error: <Results><Result ID="" Code="-2130575312"> <ErrorText>Invalid file name The file name you specified could not be used. It may be the name of an existing file or directory, or you may not have permission to access the file.</ErrorText></Result> <Result ID="" Code="-2147023673"> <ErrorText>The operation failed because an unexpected error occurred. (Result Code: 0x800704c7)</ErrorText></Result> </Results> Kindly let know how can i debug this issue.



Url

Email

Comments