OneDrive for Business: Empty Recycle Bin using PowerShell

Requirement: Empty recycle bin in OneDrive for business using PowerShell

How to Empty Recycle Bin in OneDrive for Business?
When you delete a file or folder from OneDrive for Business, it goes to the first-stage "Recycle Bin". If you delete it from the first-stage recycle bin, then the file or folder goes to the second-stage recycle bin. Deleted items in the recycle bin stays there until they're permanently removed in 93 days automatically. Within this time frame, you may have to remove them from recycle bin to free up storage space.

To reclaim storage space, you can empty the "Recycle bin" in the OneDrive Site. Here's how to empty your OneDrive's Recycle bin:
  • Login to your OneDrive site, Click on "Recycle bin" link from the left navigation.
  • Now you can see all the files and folders that were deleted in a descending list, based on the date when they were removed.
  • To empty your Recycle bin, click "Empty recycle bin" button on the top and confirm the prompt that tells you that it will delete all the items that are found in the Recycle bin, by pressing "Yes".empty recycle bin onedrive for business powershell
  • Once all files and folders are deleted from End-User recycle bin, wait for sometime and click on "Second-Stage" Recycle bin.onedrive for business empty recycle bin powershell
  • Again click on "Empty Recycle Bin" in second stage recycle bin to remove files permanently and empty your OneDrive for Business Site's recycle bin.onedrive for business recycle bin powershell
Important: Be sure you don't need any files or folders in the recycle bin before you deleting them permanently!

PowerShell to Empty Recycle bin in OneDrive for Business Site
Let's empty OneDrive for Business recycle bin using PowerShell:
#Set Parameters
$OneDriveSiteURL="https://crescent-my.sharepoint.com/personal/salaudeen_crescent_com/"

#Get Credentials to connect
$Cred = Get-Credential
 
#Connect to OneDrive for Business Site
Connect-PnPOnline $OneDriveSiteURL -Credentials $Cred

#empty recycle bin onedrive for business powershell
Clear-PnPRecycleBinItem -All -force

When you have more than 5000 items in your recycle bin, both the UI method and PowerShell above may fail with"Threshold exceeded error message. So, Here is how to clear recycle bin items in batches:
#empty first stage recycle bin in onedrive for business site
Get-PnPRecycleBinItem -FirstStage -RowLimit 5000  | Clear-PnpRecycleBinItem -Force

#PowerShell to empty 2nd Stage recycle bin in onedrive for business
Get-PnPRecycleBinItem -SecondStage -RowLimit 5000  | Clear-PnpRecycleBinItem -Force
However, You got to run the above script multiple times based on number of items in the recycle bin. Let's place it inside a while loop:
#Config Variables
$SiteURL =  "https://crescent-my.sharepoint.com/personal/salaudeen_crescent_com"

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

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

Onedrive for Business: Empty Recycle Bin for All Sites using PowerShell
How about clearing recycle bin's for all OneDrive sites in the Office 365 tenant?
#Set Parameters
$AdminSiteURL="https://crescent-admin.sharepoint.com"

#Get Credentials to connect
$Cred = Get-Credential
 
#Connect to Tenant Admin Site
Connect-PnPOnline $AdminSiteURL -Credentials $Cred
 
#Get All OneDrive for Business Sites
$OneDriveSites = Get-PnPTenantSite -IncludeOneDriveSites -Filter "Url -like '-my.sharepoint.com/personal/'"

#Loop through each site
ForEach($Site in $OneDriveSites)
{ 
    #Connect to OneDrive for Business Site
    Connect-PnPOnline $Site.URL -Credentials $Cred
    Write-Host -f Yellow "Processing Site: "$Site.URL
 
    #empty recycle bin onedrive for business powershell
    Clear-PnPRecycleBinItem -All -force
    
    Disconnect-PnPOnline
}
OneDrive for Business: Empty Recycle Bin using PowerShell OneDrive for Business: Empty Recycle Bin using PowerShell Reviewed by Salaudeen Rajack on April 10, 2019 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.