Friday, June 24, 2016

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.
  • 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
}

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



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!


You might also like:

Related Posts Plugin for WordPress, Blogger...