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 "https://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 - Information Technology Expert with Two decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. Passionate about sharing the deep technical knowledge and experience to help others, through the real-world articles!

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

  • 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
  • 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 https://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
  • thank you . excellent post

    Reply

Leave a Reply

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