SharePoint Online: Get Recycle Bin Storage Size using PowerShell

Requirement: Get the recycle bin storage space consumption of all SharePoint Online site collections

Get Recycle Bin Size in SharePoint Online using PowerShell

SharePoint Online: Find Recycle Bin Storage Size of a Site Collection

Are you curious about how much space your SharePoint Online recycle bin is taking up? The recycle bin in SharePoint Online allows you to restore deleted content such as lists, libraries, and documents within a specified period of time. As a result, the recycle bin also consumes storage space, which can adversely affect the performance and efficiency of your SharePoint Online environment.

In this post, we’ll show you how to check the size of your recycle bin with the help of PowerShell!

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
  
#Variables for Processing
$SiteUrl = "https://Crescent.sharepoint.com/Sites/Marketing"

#Setup Credentials to connect
$Cred = Get-Credential
  
Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
  
    #Get the Site collection's recycle bin: both 1st stage and 2nd stage
    $RecycleBinItems = $Ctx.Site.RecycleBin
    $Ctx.Load($RecycleBinItems)
    $Ctx.ExecuteQuery()

    #Get Size of the Recycle bin items
    $RecycleBinSize = $RecycleBinItems | Measure-Object Size -Sum

    Write-Host -f Green "Total Recycle Bin Size: $($RecycleBinSize.Sum/1MB) MB"
    
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

PowerShell to Get SharePoint Online Recycle bin Storage Size

How about finding the recycle bin size of all SharePoint Online sites in your tenant?

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.Online.SharePoint.Client.Tenant.dll"
 
#Function to Get the recycle bin Size of a SharePoint Online site collection
Function Get-SPOSiteRecycleBinSize($SiteURL, $Cred)
{ 
    #Setup credentials
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Set up the context
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL) 
    $Context.Credentials = $Credentials
   
    $Site = $context.Site
    $RecycleBin = $Site.RecycleBin
    $context.Load($Site)
    $context.Load($RecycleBin)
    $context.ExecuteQuery()

    $FirstStageSize=0
    $SecondStageSize=0

    Foreach($Item in $RecycleBin)
    {
        If($Item.itemState -eq "FirstStageRecycleBin")
        {
          $FirstStageSize+=$Item.Size
        }
        Else
        {
          $SecondStageSize+=$Item.Size
        }
    }
    #Output the results
    $Data ="Site Collection URL {0}. First Stage Recycle Bin Size {1}. Second Stage Recycle Bin Size {2}" -f $SiteURL,$FirstStageSize,$SecondStageSize
    Write-host -f Green $Data
}

#Get All Site collections from the Tenant- Including Modern Team sites and communication sites
Function Get-SPOSites($AdminSiteURL, $Cred)
{
    #Setup credentials to connect
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($AdminSiteURL)
    $Ctx.Credentials = $Credentials
 
    #Get the tenant object 
    $Tenant = New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($ctx)
 
    #Get All Site Collections
    $SiteCollections=$Tenant.GetSitePropertiesFromSharePoint(0,$true)
    $Ctx.Load($SiteCollections)
    $Ctx.ExecuteQuery()

    #Iterate through Each site collection
    ForEach($Site in $SiteCollections)
    {
        Write-host -f Yellow "Searching Site Collection:"$Site.URL
        Get-SPOSiteRecycleBinSize -SiteUrl $Site.Url -Cred $Cred
    }
}
 
#Set Parameters
$AdminSiteUrl = "https://crescent-admin.sharepoint.com/"
$Cred= Get-Credential

Get-SPOSites -AdminSiteURL $AdminSiteUrl -Cred $Cred

This script gets you the storage occupied by the first stage and the second stage recycle bins of all site collections of the tenant.

PnP PowerShell to Get Recycle Bin Size of a SharePoint Online Site Collection

You can use the Get-PnPRecycleBinItem cmdlet to view all the items in the site collection recycle bin:

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Sum Recycle bin Items Size
$RecycleBinSize = Get-PnPRecycleBinItem -RowLimit 500000 | Measure-Object -Property Size -Sum

#Get Recycle bin size
Write-host "Recycle Bin Size (MB):" ([Math]::Round($RecycleBinSize.Sum/1MB,2)) 

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

4 thoughts on “SharePoint Online: Get Recycle Bin Storage Size using PowerShell

  • Hi. This is exactly what we need. how do we export this to CSV?

    Reply
  • Great article! How do I export the results in the third script(pnp) as a CSV file?

    Reply
  • how to export this in to csv file with column of Site url, first stage recyclebin , secondstage recycle bin.
    can you please help

    Reply
  • Does file size of deleted items reflects the final version of that file or combined all versions of the file.? Just like storman.aspx reports the file sizes are of inclusive of all file versions of file

    Reply

Leave a Reply

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