SharePoint Online: Empty Recycle Bin using PowerShell

Requirement: Empty recycle bin in SharePoint Online using PowerShell

How to Empty Recycle bin in SharePoint Online?

Recycle bin capability in SharePoint Online is used to retain deleted items such as list items, documents, lists, libraries, and sites for 93 days. Deleted items in the recycle bin occupy storage and should be cleared to regain the space immediately. To clear the SharePoint Online recycle bin at the site level, follow these steps:

  • Navigate to your SharePoint Online site, Click on Site Settings Gear >> Choose “Site Contents”
  • From the “Site contents” page, click on the “Recycle Bin” link in the top-right corner. You can also access the Recycle bin from quick launch navigation (if it’s available!)empty sharepoint online recycle bin using powershell
  • You can click on the “Empty recycle bin” button in the toolbar now! Or you can select all items and click on the “Delete Selection” link to delete items from recycle bin in SharePoint Online. This moves items to the second stage recycle bin.
  • Empty site collection recycle bin SharePoint Online: If you have the site collection Admin rights, you can use the Recycle bin link at Site Settings >> Recycle bin (Under Site collection Administration) to access deleted items of all users and all sites in the site collection.
  • Empty second stage recycle bin in SharePoint Online: Once the first stage recycle bin is cleared, You can click on the “Second-stage recycle bin” link to access the second stage recycle bin (Shortcut: /_layouts/15/AdminRecycleBin.aspx?view=13) and empty that as well.
    sharepoint online recycle bin powershell

BTW, You should have at least “Edit” access rights to access and restore deleted content from the recycle bin.

SharePoint Online: Empty Recycle bin using PowerShell

So, we have emptied the SharePoint Online recycle bin manually. But what if there was a way to automate it? PowerShell to the rescue! I’ll show you how to use PowerShell to empty your SharePoint Online recycle bin. Here is the PowerShell to empty recycle bin in SharePoint Online:

#Load SharePoint Online 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/Sales/"
$UserName="[email protected]"

#Get the password to connect 
$Password = Read-host -assecurestring "Enter Password for $UserName"
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,$Password)
 
Try {    
    #Setup the context
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $Context.Credentials = $Credentials
    
    #Get the recycle bin
    $Site = $Context.Site
    $RecycleBinItems = $Site.RecycleBin
    $Context.Load($Site)
    $Context.Load($RecycleBinItems)
    $Context.ExecuteQuery()

    Write-Host "Total Number of Items found Recycle Bin:" $RecycleBinItems.Count
    #sharepoint online powershell empty recycle bin
    $RecycleBinItems.DeleteAll()
    $Context.ExecuteQuery()
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

This permanently deletes all items from both first-stage, and second-stage recycle bins of the site collection.

SharePoint Online: Empty First Stage Recycle Bin

At times, You may have to delete items from either of the recycle bin. Use these scripts if you want to empty the first stage or the second stage recycle bins:

PowerShell to Delete from recycle bin

Try {    
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $Ctx.Credentials = $Credentials
    
    #Move All Deleted Items to 2nd Stage Recycle bin
    $Ctx.Site.RecycleBin.MoveAllToSecondStage()
    $Ctx.ExecuteQuery()
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

PowerShell to Empty Second Stage Recycle bin in SharePoint Online

How do I delete files from the second stage Recycle Bin? Well, To empty the site collection’s second stage recycle bin, use this PowerShell script:

Try {    
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $Ctx.Credentials = $Credentials
    
    #Delete All Items from 2nd Stage Recycle bin
    $Ctx.Site.RecycleBin.DeleteAllSecondStageItems()
    $Ctx.ExecuteQuery()
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

This empties the site collection recycle bin in SharePoint Online.

SharePoint Online: PowerShell to Delete Site from Recycle Bin

To delete a site collection from recycle bin permanently, use:

$Cred = Get-credential
Connect-SPOService -url "https://crescent-admin.sharepoint.com/" -credential $Cred
Get-SPODeletedSite | Select URL 

To Remove Deleted SharePoint Online Site Collection permanently:

Remove-SPODeletedSite -identity $DeletedSiteCollURL -Confirm:$False

SharePoint Online: Empty Recycle bin using PnP PowerShell

To clear both the first stage and second stage recycle bins, use this PowerShell script:

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

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

#Empty Reycle Bins: Both 1st stage and 2nd Stage
Clear-PnPRecycleBinItem -All -Force

How about clearing recycle bin items with 5000+ deleted items? Well, to avoid the threshold exceeded issue, we’ll have to empty deleted items in batches. Here is how:

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

#Connect to Tenant Admin Site
Connect-PnPOnline -Url $SiteURL -Interactive

#Get recycle bin items in batches and delete them permanently
While( (Get-PnPRecycleBinItem -RowLimit 500) -ne $null)
{
    Get-PnPRecycleBinItem -RowLimit 500 | Clear-PnPRecycleBinItem -Force
}

To empty the second-stage recycle bin, use -SecondStageOnly switch. You can also use: Move-PnpRecycleBinItem cmdlet to clear the first stage recycle bin. Here is my another post for SharePoint On-Premises: PowerShell to Empty Recycle bin in SharePoint

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!

15 thoughts on “SharePoint Online: Empty Recycle Bin using PowerShell

  • Use the below instead –

    #Get recycle bin items in batches and delete them permanently
    While( (Get-PnPRecycleBinItem -SecondStage -RowLimit 500) -ne $null)
    {
        Clear-PnPRecycleBinItem -SecondStageOnly -RowLimit 500 -Force
    }
    
    Reply
  • Thanks Nico! You’re a life saver

    Reply
  • the cmdlets changed, the command should now be:
    #Get recycle bin items in batches and delete them permanently
    While( (Get-PnPRecycleBinItem -RowLimit 500) -ne $null)
    {
    Clear-PnPRecycleBinItem -RowLimit 500 -All -Force
    }

    Reply
  • Same here, -rowlimit not working

    Reply
  • I am unable to run the script due to the following exception:
    The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator.

    Reply
    • When you have more than 5000 deleted items, use: -RowLimit parameter as in the PnP PowerShell example in this article.

      Reply
    • I tried using -rowlimit switch, still got the error “The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator.”
      Even with a row limit set to 50…

      Reply
    • Same here even set with 1…

      Reply
    • I have the same issue :(, could somebody find the fix?

      Reply
    • Did anyone have an answer to this?

      Reply
    • Be sure you have the latest PnP PowerShell module! You can update your existing module with:
      Update-Module SharePointPnPPowerShellOnline

      Reply
  • Very helpful. I used the first script and it worked perfectly! Thanks Salaudeen.

    Reply
  • Is there a cmdlet for emptying second-stage recycle bin?

    Reply
    • Use: $Ctx.Site.RecycleBin.DeleteAllSecondStageItems() or Clear-PnPRecycleBinItem -SecondStageOnly

      Reply

Leave a Reply

Your email address will not be published.