Get SharePoint Recycle Bin Storage Size using PowerShell

In a Database attachment method migration project, wanted to get the insights on how much space being occupied by SharePoint Admin Recycle bin for all site collections. There were 3000+ site collections.

PowerShell Script to get First Stage recycle bin size:

function global:Get-SPSite($url) {
    return new-Object Microsoft.SharePoint.SPSite($url)
 } 
 
function Get-RecyclebinSize($SiteURL)
{   
    #Get the site collection
    $site = Get-SPSite $SiteURL
     
    # Create SPRecycleBinQuery object to Query Reycle bin
    $SPRecycleBinQuery = New-Object -TypeName Microsoft.SharePoint.SPRecycleBinQuery
    $SPRecycleBinQuery.ItemState = [Microsoft.SharePoint.SPRecycleBinItemState]::FirstStageRecycleBin
    
    # Set the query should return maximum number of objects 
    $SPRecycleBinQuery.RowLimit = [int]::MaxValue-1
    
    # Get the sum of values of the 'Size' property of all Recycle Bin items and assign it to a variable
    $RecycleBinSize = $site.GetRecycleBinItems($SPRecycleBinQuery) | Measure-Object -Property Size -Sum | Select-Object -ExpandProperty Sum
    
    # Calculate the value in Mbs and returned the rounded value to the caller       
    return ([System.Math]::Round(($RecycleBinSize/1Mb),2))
}

 $Size = Get-RecyclebinSize "https://intranet.crescent.com/sites/sales"
 write-host "First Stage Recycle bin Size (in MB):" $size 

Here is the PowerShell script to get the 2nd stage recycle bin’s size in SharePoint 2013:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue 

$WebApp=get-spwebapplication "http://sharepoint.crescent.com"

foreach ($SPSite in $WebApp.Sites)
{       
	# SPRecycleBinQuery to Query Reycle bin    
	$SPRecycleBinQuery=new-object Microsoft.SharePoint.SPRecycleBinQuery
	$SPRecycleBinQuery.OrderBy = [Microsoft.SharePoint.SPRecycleBinOrderBy]::DeletedDate;
	$SPRecycleBinQuery.IsAscending = $false;
	$SPRecycleBinQuery.RowLimit = 2000
	$SPRecycleBinQuery.ItemState = [Microsoft.SharePoint.SPRecycleBinItemState]::SecondStageRecycleBin

	$SPRecycleBinItemCollection  = $SPSite.GetRecycleBinItems($SPRecycleBinQuery)

	$Size=0

	for ($i=$SPRecycleBinItemCollection.Count-1; $i -GE 0;  $i--)
		{
			$guid = $SPRecycleBinItemCollection[$i].ID;
			#$SPRecycleBinItemCollection.Restore($guid);
			#$SPRecycleBinItemCollection.Delete($guid);
			$Size+=$SPRecycleBinItemCollection[$i].Size				
		}
	write-host "Recycle bin Size in" $SPSite.RootWeb.Title "-"  $SPSite.RootWeb.URL ":" ($size/1MB)   
}

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

3 thoughts on “Get SharePoint Recycle Bin Storage Size using PowerShell

  • August 16, 2016 at 7:53 AM

    Hello,

    Can you please help me in modifying the script to consolidate both first stage and second stage recycle bin into single script and export to csv. Its bit urgent.

    Thanks in Advance.

    Reply
  • August 27, 2015 at 7:10 PM

    Salaudeen,
    Great post. I suggest an update for the second stage recycle bin size script. When you set the row limit to a value you need to cycle the collection until all the items are counted. In the above example you will only find the size of the first 2000 items.

    $SPSite = Get-SPSite http://webapp/site
    #SPRecycleBinQuery to Query Reycle bin
    $SPRecycleBinQuery=new-object Microsoft.SharePoint.SPRecycleBinQuery
    $SPRecycleBinQuery.RowLimit = 2000
    $SPRecycleBinQuery.ItemState = [Microsoft.SharePoint.SPRecycleBinItemState]::SecondStageRecycleBin
    $Size=0
    DO
    {
    $SPRecycleBinItemCollection = $SPSite.GetRecycleBinItems($SPRecycleBinQuery)
    $SPRecycleBinQuery.ItemCollectionPosition = $SPRecycleBinItemCollection.ItemCollectionPosition

    For($i=$SPRecycleBinItemCollection.Count-1; $i -GE 0; $i–)
    {
    $Size += $SPRecycleBinItemCollection[$i].Size
    }
    }
    While ($SPRecycleBinQuery.ItemCollectionPosition -Ne $Null)
    Write-Host “Recycle bin size in” $SPSite.RootWeb.Title “-” $SPSite.RootWeb.URL “:” ($size/1MB)

    Reply
  • February 21, 2014 at 9:54 PM

    thank you . excellent post

    Reply

Leave a Reply