kwizcom banner advertisement

SharePoint Online: Empty Recycle Bin using PowerShell

Requirement: Empty recycle bin in SharePoint Online using PowerShell.

To clear SharePoint online recycle bin at site level, follow these steps:
  • Navigate to your SharePoint online site, Click on Site Settings Gear >> Choose "Site Contents"
  • From the All site contents page, click on "Recycle Bin" link
    sharepoint online empty recycle bin powershell
  • Select all items and click on "Delete Selection" link to delete items from recycle bin in SharePoint online. This moves items to second stage recycle bin.
  • If you have Site collection Admin rights, you can use Recycle bin link at Site Settings >> Recycle bin (Under Site collection Administration) to access deleted items of all users and second stage recycle bin. 
    sharepoint online recycle bin powershell
SharePoint Online: Empty Recycle bin using PowerShell
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="Salaudeen@crescent.com"

#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
    $RecycleBinItems.DeleteAll()
    $Context.ExecuteQuery()
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
This permanently deletes all items from both end-user (first stage) recycle bin and site collection Administrator (second stage) recycle bin.


SharePoint Online: Empty First Stage (End-user) and Second Stage (Site Collection) Recycle Bin
At times, You may have to delete items from either of the recycle bin. Use these scripts if you want to empty first stage or 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: 
To empty site collection 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
}

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

Here is my another post for SharePoint On-Premises: PowerShell to Empty Recycle bin in SharePoint
SharePoint Online: Empty Recycle Bin using PowerShell SharePoint Online: Empty Recycle Bin using PowerShell Reviewed by Salaudeen Rajack on June 24, 2016 Rating: 5

1 comment:

  1. Is there a cmdlet for emptying second-stage recycle bin?

    ReplyDelete

Please Login and comment to get your questions answered!

Powered by Blogger.