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.

Efficiently Delete / Purge All Items from a SharePoint List
  • 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.

  • 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.

  • 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…

  • Erich

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

    Thanks.

  • 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?

  • 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

  • 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.

  • 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?

  • 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.

  • 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.

  • 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

  • Pingback: Uncatchable Spark » SPWeb.ProcessBatchData throws “Value does not fall within the expected range” exception()

  • Leo

    This helped us out tremendously. Thanks for sharing!

  • 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!

  • Ethan Deng

    There is a limitation on the size of the query you can build with this method.

  • I totally agree the standpoint of upstairs, and I believe this will be a trend. I often come this forum , rom here I learn much and know the newest tide! the content here constantly update shoe and I love it! Another I know some websites which often update their contents, you guys should browse if you are free. http://inin-from.com

  • Mark S. is definitely on the right track. If you want to get a professional looking email address, Id recommend buying your name domain name, like or
    ajf 8
    If its common it might be difficult to get, however, be creative and you can usually find something.

  • Thanks a lot Merill.

  • Aliaksandr

    There is also a good post at http://sharepoint-in-action.blogspot.com/2011/06/delete-items-from-sharepoint-list-and.html – delete items from sharepoint list in threads.

  • guru

    if a list has 150 columns and 50000 records.. stringbuilder wont crash? same way if i try to insert item, around 25000 records with columns 160.. wat is the capacity on string builder?
    one more doubt
    i m iterating 15000 records from excel sheet with some logic i m building xml ith 2000 records and giving it to web.processbatchdata, until it creates those items the code wont execute further to process records.. how can i add this is thread? so that i can process all data in parallel?