Saving time, once again, with PowerShell
Have you ever ended up with a very large number of items in your SharePoint Site Collection recycle bin?
Sometimes, certain projects that involve migrations or large changes can fill up your bin with all of the items that didn’t make it to the next round.
More than likely, your recycle bin was similar to mine and only displays 50 items per page. For a recycle bin which contains thousands of items, I don’t have to do the math for you to realize that it would take a very long time to empty page by page.
This can happen for any number of reasons, but I’ll share how I ended up with such a large recycle bin and how I ultimately emptied the entire thing in just minutes.
How I overloaded my Recycle Bin:
I was recently performing a migration from WSS 2007 to SharePoint 2010 using our third-party migration tool, the Metavis Migrator.
During the migration, I ran into a few issues in regards to user mappings. Working with the fantastic Metavis Support Team, we resolved the issue and could successfully perform the migration again. But this particular site had a list with over 27,000 items in it, and because I had to attempt the migration of this site multiple times, I ended up with over 70,000 items in the recycle bin!
Like I said above, that would take a very, very long time only emptying 50 items at a time.
How did I empty the entire Recycle Bin?
One word – PowerShell.
Empty entire Site Collection Recycle Bin using PowerShell:
- Browse to the Start Menu. Search for “PowerShell”, select Windows PowerShell ISE. (I am running Windows 2012 in this example. The steps to open PowerShell ISE may differ if using another Operating System).
- Copy the below command into PowerShell. Update the Site Collection URL to match the appropriate site.
| $sitecollectionUrl = “http://domainname.com/sitecollectionurl”$siteCollection = New-Object Microsoft.SharePoint.SPSite($sitecollectionUrl)write-host(“Items to be deleted : ” +$siteCollection.RecycleBin.Count.toString())$now = Get-Datewrite-host(“Deleting started at ” +$now.toString())$siteCollection.RecycleBin.DeleteAll();$now = Get-Date
write-host(“Deleting completed at ” +$now.toString())
- Once you have updated the Site Collection URL, execute the command by clicking on the Run Script button on the PowerShell toolbar.
Depending on how many items are in the recycle bin, this could take a few minutes. When the command is executing, the red stop button (two to the right of the Run Script button) will be illuminated. When the command is completed, the Run Script button will be illuminated once again.
When the command is completed, you can verify if it was successful. Chances are, if the command did not display any errors, all items have been deleted. Simply browse to the appropriate SharePoint site to determine if the Site Collection recycle bin is now empty.
As stated above, the recycle bin I was working with had approximately 70,000 items. I executed the command, knowing it would take some time. I checked back about 20 minutes later and it was completed. An easy way to check progress is by monitoring the recycle bin while the command is executing. You will be able to refresh the page and watch the number of items decrease.
That’s it! Please comment below with any questions.