Thursday, August 30, 2012

Empty SharePoint Recycle Bins Programmatically with PowerShell

In a Database attachment method migration project, we decided to clear all the recycle bins to get some additional free space in databases. There were 3000+ site collections. Automation?

SharePoint Automation = PowerShell  :-)

Empty SharePoint Site Collection Recycle bin using PowerShell:
#Get the Site Collection
$Site = Get-SPSite ""

#Delete all from 1st Stage Recycle bin
$Site.AllWebs | Foreach-object { $_.RecycleBin.MoveAllToSecondStage() }

#Empty 2nd Stage Recycle bin

PowerShell Script to Empty SharePoint Recycle Bins Programmatically:
Lets empty the recycle bin of all site collection under a given web application using PowerShell.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue 

$WebApp=Get-SPWebApplication ""

    foreach ($SPSite in $webApp.Sites)
      #get the collection of webs
      foreach($SPWeb in $SPSite.AllWebs)
             #Empty the 1st Stage Recycle bin items PERMENANTLY
             #Send the 1st Stage Recycle bin items to 2nd Stage

              write-host "End-User Recycle Bin Items Deleted for:" 
              write-host $SPWeb.title ":" $SPWeb.URL "`n"

              #Dispose Web object
             #Empty SharePoint site collection recycle bin (Second Stage Recycle bin) or Admin Recycle bin

             #Dispose Site object

    write-host "Administrator Recycle bin Items Deleted for:" $SPSite.RootWeb.title "`n"

Above code is fairly simple, and can be rewritten in C# also.

You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.

Check out these SharePoint products:


  1. How do you run this Powershell in Sharepoint 2007?

  2. Nice script, thanks for sharing.
    But I have to note that it leaks memory as it does not dispose the SPSite and SPWeb objects. Details can be found here:

  3. Can this be modified to run on a single site collection and not the entire Web App?


Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...