Purge List Items in SharePoint 2010
In August 2012 my company migrated to SharePoint 2010, and immediately ran into a problem. SharePoint 2010 has a limit on lists of 5000, and this is a pretty hard limit–it won’t even show the list items to the user if you’re over the threshold. Now, you could just go in and change the threshold to a much higher number but honestly, that completely defeats the whole purpose of the threshold which is there to improve list performance. Ideally, you want to delete items in the list until you’re under the threshold. Here’s how I did it.
Not too hard to solve this problem, just create a view based on a date field in the list and put a filter in of [Today]-xx where xx is the number if days you want to keep. Select all and delete! Of course, nothing is ever that easy and I immediately ran into a problem. When selecting you can only select 100 items at a time, which is not too bad when you have 700 records to remove but pretty daunting when you have 14,000 (which I did). PowerShell to the rescue, again!
I first started by adapting this code I found here. The original code is a bit rough but I got it working after a bit. Started running it on about 450 items and it was running SLOW! Like after 5 minutes I stopped the script and discovered it had removed about 100 items. That 14,000 item list delete was not looking too good! But, while looking around I did stumble across another scriptblock that just might do the trick. Had to do some searching on how to do caml queries, which wasn’t too bad and a ton of testing to make sure I didn’t screw up too badly and I got it working. One thing I had run across on the various searches I had made was keeping these batches somewhat small for performance reason’s, so I limited the query to 1000 records and then set a loop to query the next 1000 and the next until all of the records were removed.
But I really wanted to be able to make it friendly and very usable, so that meant making a function out of it. That way you can pull the function code out of it and use it in other scripts if you want or pretty much do whatever you want to it. I then wrapped some logging functionality around it so you knew what it was doing and then email the results so you can see what it’s done.
And with that, I’d like to introduce PurgeListByDate.ps1
Everything is run within the script but you can easily pull the function code out and add it to you PowerShell profile or put it in another script. The function itself takes 5 required parameters:
- spWeb: the full URL to your SharePoint root site. IE http://sharepoint
- spSite: the path to the site where the list is loctaed. IE “/” for the root site or “/itdept”, etc.
- spList: full name of the list you want to purge. IE “Backup Log”
- spField: name of the field you want to use. This field must be a DateTime type field. IE “Backup On”
- spKeep: number of days you want to keep. IE 90. This would mean the function will delete any list item where the spField (“Backup On”) is 91 days or older.
PurgeList -spWeb “http://sharepointsite” -spSite “/itdept” -spList “Backup Log” -spField “Backup On” -spKeep 90
Rate this:
Share this:
Related
August 7, 2012 - Posted by Martin9700 | PowerShell | list, purge, purge list, Sharepoint, Sharepoint 2010, Technical
2 Comments »
Leave a Reply Cancel reply
Blog Search
Blogroll
Links
Categories
- General (22)
- Humor (1)
- MS SQL (3)
- PowerShell (78)
- Powershell – Best Practices (6)
- Powershell – Getting Started (9)
- PowerShell – HTML Reporting (6)
- Powershell – Performance (12)
- Random Thoughts (8)
- Technical (17)
Top Posts & Pages
- Multithreading Powershell Scripts
- Generate Barcodes in Powershell
- Report All Snapshots in Your VMware Environment
- Using Powershell as a Telnet Client
- Getting Last Logon Information With PowerShell
- Discovery with Powershell - Script to Get Screenshots for VMs
- Getting Directory Information Fast
- Get File Counts and Size for All Extensions
Archives
- March 2021 (1)
- May 2019 (1)
- February 2019 (1)
- November 2016 (2)
- August 2016 (1)
- March 2016 (1)
- February 2016 (2)
- January 2016 (1)
- December 2015 (1)
- November 2015 (1)
- October 2015 (1)
- June 2015 (2)
- May 2015 (1)
- April 2015 (2)
- March 2015 (1)
- January 2015 (4)
- December 2014 (1)
- September 2014 (4)
- August 2014 (1)
- July 2014 (4)
- June 2014 (3)
- April 2014 (3)
- March 2014 (2)
- February 2014 (2)
- January 2014 (4)
- December 2013 (3)
- November 2013 (1)
- October 2013 (3)
- August 2013 (2)
- May 2013 (3)
- April 2013 (6)
- March 2013 (7)
- February 2013 (8)
- January 2013 (10)
- December 2012 (9)
- November 2012 (7)
- October 2012 (12)
- September 2012 (9)
- August 2012 (4)
- April 2011 (1)
- August 2010 (2)
- April 2010 (1)
- January 2010 (1)
- December 2009 (1)
- September 2009 (2)
- August 2009 (1)
- July 2009 (2)
Tag Cloud
ACL Active Directory Add-Member adsi Advanced Function alan rickman Background Jobs Best Practices ConvertTo-HTML Credentials csv Dfs dfs monitor dfs monitoring Discovery email employee directory Exchange Exchange Implicit Remoting Foreach ForEach-Object function Functions Get-ChildItem get-command get-help Get-Member GitHub Google Maps Google Visualizations Hashtable hbo html html report HTML Reports Import-SPWeb Invoke-SQLQuery Jobs JSON Meraki Modules monitoring mos def movie review Multi-thread multithread New User New User Automation object Pebble performance pipeline powergui PowerShell Powershell Get Started PowerShell Jobs PSObject Pushover regex Remoting Reporting RESTful ScriptProperty secure credentials Set-AlternatingRows Sharepoint spiceworks Spiceworld sql Strings Technical Veeam VMware WMI Write-Progress
Follow me on Twitter
My TweetsMe
Martin9700
Father, husband, IT Pro, scripter, blogger, cancer survivor. I tweet and blog about my IT passions, Powershell chief among them. Very active at Spiceworks.
Spiceworks
LinkedIn
RSS
-
Join 374 other subscribers
Hi
Thanks for a nice script. Only that when it is being runned it doesn’t remove the objects in the list. The mail says it has purged some items, but only in theory. Have tried every possible account to resolve/run it but no go. Any PowerShell rights that has to be consider?
So other then the fact that it doesn’t work, it looks good? I’m a little busy right now with a crashed server but as soon as I get some air I will look at it!