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:
  1. Dionisis Athanitis
    April 17th, 2008 at 00:37 | #1

    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
    May 9th, 2008 at 19:15 | #2

    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
    May 9th, 2008 at 19:38 | #3

    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
    May 13th, 2008 at 18:51 | #4

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

    Thanks.

  5. Thomas
    May 28th, 2008 at 07:23 | #5

    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. July 23rd, 2008 at 23:23 | #6

    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
    August 12th, 2008 at 05:13 | #7

    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. January 26th, 2009 at 17:41 | #8

    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
    May 22nd, 2009 at 00:20 | #9

    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
    May 22nd, 2009 at 00:21 | #10

    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
    August 31st, 2009 at 06:07 | #11

    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. Leo
    October 19th, 2009 at 22:46 | #12

    This helped us out tremendously. Thanks for sharing!

  13. Radi A.
    December 23rd, 2009 at 23:05 | #13

    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!

  14. Ethan Deng
    May 28th, 2010 at 19:46 | #14

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

  15. June 3rd, 2010 at 01:37 | #15

    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

  16. July 5th, 2010 at 06:51 | #16

    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.

  17. February 18th, 2011 at 06:08 | #17

    Thanks a lot Merill.

  18. Aliaksandr
    June 21st, 2011 at 01:13 | #18

    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.

  1. September 30th, 2009 at 05:43 | #1