Efficiently Delete / Purge All Items from a SharePoint List

If you want to delete or remove all the items in a SharePoint list you need to iterate through each item and perform a delete. Most of the examples on the web iterate through each item to do the delete. The problem there is that each delete action makes a request to the server and a huge list is going to take a long time. You could always delete the entire list and recreate it but that would mean recreating the structure as well as the guid being changed.

The better method would be to make a single request with all the delete commands batched together as shown in the code sample below.

   1: private static StringBuilder BuildBatchDeleteCommand(SPList spList)
   2: {
   3:     StringBuilder sbDelete = new StringBuilder();
   4:     sbDelete.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");
   5:     string command = "<Method><SetList Scope=\"Request\">" + spList.ID + 
   6:         "</SetList><SetVar Name=\"ID\">{0}</SetVar><SetVar Name=\"Cmd\">Delete</SetVar></Method>";
   7:  
   8:     foreach (SPListItem item in spList.Items)
   9:     {
  10:         sbDelete.Append(string.Format(command, item.ID.ToString()));
  11:     }
  12:     sbDelete.Append("</Batch>");
  13:     return sbDelete;
  14: }

I’ve published a working solution on the MSDN Code Gallery at http://code.msdn.microsoft.com/SharePointListDelete.

14 Responses to “Efficiently Delete / Purge All Items from a SharePoint List”

  1. Dionisis Athanitis Says:
    April 17th, 2008 at 12:37 am

    You are the best!!!
    After spending so many hours searching for a relative solution I found for post.
    You are really a life saver!!!

    Thanks a lot.

  2. Kev Says:
    May 9th, 2008 at 7:15 pm

    This is a very nice, relatively fast and efficient way to delete all items from a sharepoint list. Good job.

    I noticed that when this method is used, the deleted items are stored in the recycle bin. Is there a way to programatically delete them from there too? Is there a way to access the recycle bin (or even the site collection recycle bin) and delete the items we want, by program?

    Thanks a lot for this post and thank you in advance for the answer of my question, if possible.

  3. Kev Says:
    May 9th, 2008 at 7:38 pm

    Actually, I will answer my own question, and hopefully it will help others too. Few minutes after posting my previous comment I found the solution…

    The following will delete all items from the user’s recycle bin:

    mySpWeb.RecycleBin.DeleteAll();

    mySpWeb is the name of the SPWeb object I’m using inside my program. But you have to be sure that you dont have other important items in your recycle bin.

    Hope this helps…

  4. Erich Says:
    May 13th, 2008 at 6:51 pm

    is it possible to (and how) include some qualification?
    for example: all items created before *date*

    Thanks.

  5. Thomas Says:
    May 28th, 2008 at 7:23 am

    Is there a way to delete the items without SP copying them to the Recycle Bin? I’m guessing this could speed up the delete even more?

  6. Yogie Says:
    July 23rd, 2008 at 11:23 pm

    Hi,

    I need to purge a single SP list, which will be filled up by a script afterwords. Unfortunately I’m not a programmer, so I need an EXE file or something else I could use, is there a ready to use solution available?

    Many thanks in advance….

    Yogie

  7. Christopher King Says:
    August 12th, 2008 at 5:13 am

    Note also that when the items that you are deleting are in a document library that you also need:
    <SetVar Name="owsfileref">{server-relative-url}</SetVar>

    where server-relative-url is the value from the SPFile instance’s ServerRelativeUrl property.

    Otherwise the ProcessBatchData call does nothing.

  8. steve Says:
    January 26th, 2009 at 5:41 pm

    Hey Yogie, so I’m writing an SP timer job that runs hourly purges all items in the list then adds new items back into the list right now. But i’m running into an issue, it takes bout 2 minutes to purge all the items. strange thing is it seems to just be appending more and more records instead of clearning out the 1000 items and adding 1000 new ones. Any one find a workaround? maybe break it up into 2 timer jobs: one to delete the files then one to add new ones?

  9. RC Says:
    May 22nd, 2009 at 12:20 am

    Yogie. I am not a programmer either. So what i did is that i imported the list into access2007. Then i delete all the items using a sharepoint query and synchronized it back to Sharepoint.
    Hope this helps.

  10. RC Says:
    May 22nd, 2009 at 12:21 am

    Yogie. I am not a programmer either. So what i did is that i imported the list into access2007. Then i delete all the items using an access delete query pointed back at sharepoint list and synchronized it back to Sharepoint.
    Hope this helps.

  11. Glynn Says:
    August 31st, 2009 at 6:07 am

    Any thoughts on how your code would perform compared to using a delete query to a linked SharePoint table in an Acess2007 app, which is terribly slow. Also, how would one go about integrating your code with an Access2007 app.

    Thanks,

    Glynn

  12. Uncatchable Spark » SPWeb.ProcessBatchData throws “Value does not fall within the expected range” exception Says:
    September 30th, 2009 at 5:43 am

    [...] every 2-3 seconds. That made me looking for alternative approaches and eventually I came across http://merill.net/2008/02/efficiently-delete-purge-all-items-from-a-sharepoint-list/ – looked like a solution to my problem, so I took the idea and built my own function using [...]

  13. Leo Says:
    October 19th, 2009 at 10:46 pm

    This helped us out tremendously. Thanks for sharing!

  14. Radi A. Says:
    December 23rd, 2009 at 11:05 pm

    Thanks Merill, I used you helper class today. I somehow remembered that you had such a post and looked it up (:

    All the best and happy Christmas holidays!

Leave a Reply

Archives