Home > SharePoint > Efficiently Delete / Purge All Items from a SharePoint List

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.

Categories: SharePoint Tags:
  • Ethan Deng
    There is a limitation on the size of the query you can build with this method.
  • Radi A.
    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!
  • Leo
    This helped us out tremendously. Thanks for sharing!
  • Glynn
    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
  • RC
    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.
  • RC
    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.
  • 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?
  • Christopher King
    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.
  • 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
  • Thomas
    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?
  • Erich
    is it possible to (and how) include some qualification?
    for example: all items created before *date*

    Thanks.
  • Kev
    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...
  • Kev
    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.
  • Dionisis Athanitis
    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.
blog comments powered by Disqus