Powershell: Efficiently empty large SharePoint lists

03 May 2013
May 3, 2013

Previously I had a little console app to efficiently empty large SharePoint lists. Today I converted it to powershell.

Compared to the simple item by item delete with item.Delete() its 30 times faster; on my dev machine it deletes ~30 items per second. It works for SharePoint 2010 and should  for 2013 (not tested, yet).

Script

Usage

Delete-Items.ps1 -weburl [url of web] -listname [name of list]

Further improvements

I think if the list is really huge (100k items or greater) the items should be deleted in batches.

I have not tried it with a document library, maybe there are issues. If there are issues, please leave a comment!

Disclaimer

There is no way back – if you start the script there is no “ARE YOU SURE?” – all data is gone in a very short time.

Max Melcher
Follow me!

Max Melcher

Maximilian Melcher (MCSE, MCPD) is a Principal Consultant working at Alegri International Services in Munich, Germany. Max is a specialist in SharePoint technologies focused on search, social computing, web content management and collaboration. Max has led SharePoint implementations for Dax 30 companies since 2009.
Max’s free time is spent on twitter (@maxmelcher) mostly with a good coffee in his hands.
Max Melcher
Follow me!
Tags: , ,
22 replies
  1. Anatoly Mironov says:

    Hi. Cool post. Removing list items with “Batch” is really fast. For a year ago I measured it. The average speed is under 30 ms per item. See the whole comparison on my blog post: Batch remove

    Reply
  2. Samuel Levesque says:

    Hello, I have tested it in SP2013 and it worked like a charm. Thx for your post !

    Reply
  3. Tristan England says:

    Thanks for the great script. I have a list that, thanks to a looping workflow, has grown to 36 million lines. How would you delete the items in batches per your further improvements?

    Reply
  4. Jeff Robertson says:

    Works great with sharepoint 2013
    Quick question, is there a way to edit this so that it deletes all items older than 1 week?
    I have a number of sites that have lists that our QA department sends log emails to, they get hundreds per day. They only need a weeks worth of items in that list. I have a script using Delete() to do this, but it is slow. I used yours to trim them down from the 50k + items they had before we started trimming, so that my slow script didnt have to much to do

    Reply
  5. Dinos says:

    to delete documents from libraries this:

    is also necessary.

    Nice and clear post btw…

    Reply
  6. Jonas says:

    Thanks for this script!
    Why is such an useful functions like emptiing a list not included OOTB? (Kind of a rethoric question… but maybe there is a reasonable answer?)

    Reply
  7. Christoph Hannappel says:

    Hi,

    nice script and the deletion is definitly faster than calling $list.item[id].delete(). Just a Warning on large lists where the deletion behaviour of your script could backfire.
    I did a dry run and the items were moved into the recycle bin, which is a deletion based on the GUI Language.

    I have a customer and they had a logging list which was trimmed to 10k entries. So all the older elements were moved into the recycle bin. Everything worked fine until they added a lot of items in a short time frame. They endet up with 36 million items in the recycle bin.
    Unfurtunatly the sharepoint recycle bin timerjob isn’t very good with a very large recycle bin.
    Result is that daily timerjob doesnt finish anymore and also consumed 10 of 14 GB SQL Server Memory. Which brakes the recycle bin clean up for every site collection inside the web app.
    Depending on the farm speed a recycle bin clean up can take years. Ours would have taken 7,1 Years. You’ll end up in deleting the content database since a site move will also fail and recreate the site collection.

    So your recommandation to do a list backup without the content is the way to go with very large lists!

    Reply
  8. ManojK says:

    Nice Post Max ! just a quick quiz, how to deploy this Script ? can I run directly from PS ISE ? Please share the execution method too.

    Reply
  9. Shoaib says:

    Hi,
    Tested it on a list with more than 200k items worked perfectly

    Reply

Trackbacks & Pingbacks

  1. […] ago I created a PowerShell script that I used a couple of times by now: efficiently empty a large SharePoint list – but apparently that code does not work against a SharePoint Online […]

  2. […] Powershell: Efficiently empty large SharePoint lists […]

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close