In this delete recycle bin using the SharePoint PowerShell tutorial, we will learn about how to delete items from the recycle bin in SharePoint using the PowerShell script. By the way, there are two types of recycle bins in SharePoint: user-level recycle bins (first-stage recycle bins) and admin-level recycle bins (second-stage recycle bins).
What is Recycle Bin in SharePoint (SharePoint Recycle Bin)?
As SharePoint users, when we delete items from a SharePoint site, they are sent to the site recycle bin (also called the first-stage recycle bin), where we can restore them if we want to. When we delete items from a site recycle bin, they are sent to the site collection recycle bin (also called the second-stage recycle bin).
A SharePoint site collection administrator can view and restore deleted items from the site collection Recycle Bin to their original locations. If an item is deleted from the site collection Recycle Bin or exceeds the retention time, it is permanently deleted, meaning we cannot restore those deleted items.
How long are deleted items kept in the SharePoint Recycle Bin?
In SharePoint, items are retained for 93 days from the time you delete them from their original location. They stay in the recycle bin the entire time unless someone deletes them from there or empties that recycle bin. In that case, the items go to the site collection Recycle Bin, where they stay for the remainder of the 93 days unless:
- The site collection When the Recycle Bin reaches its quota, it begins to purge the oldest items.
- The items are manually deleted by the site collection administrator from the site collection Recycle Bin.
Delete recycle bin items in SharePoint using PowerShell – SharePoint Recycle Bin
Using the below PowerShell script we can remove the items from the recycle bin in SharePoint:
#The below script is used to remove the items from Recycle Bin using the PowerShell script. cls $PSshell = Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorVariable err -ErrorAction SilentlyContinue if($PSshell -eq $null) { Add-PSSnapin "Microsoft.SharePoint.PowerShell" } $fileName = "RecycleBinCleanUpReport" #'yyyyMMddhhmm yyyyMMdd $enddate = (Get-Date).tostring("yyyyMMddhhmmss") #$filename = $enddate + '_VMReport.doc' $logFileName = $fileName +"_"+ $enddate+"_Log.txt" $invocation = (Get-Variable MyInvocation).Value $directoryPath = Split-Path $invocation.MyCommand.Path $directoryPathForLog=$directoryPath+"\"+"LogFiles" if(!(Test-Path -path $directoryPathForLog)) { New-Item -ItemType directory -Path $directoryPathForLog #Write-Host "Please Provide Proper Log Path" -ForegroundColor Red } #$logPath = $directoryPath + "\" + $logFileName $logPath = $directoryPathForLog + "\" + $logFileName $isLogFileCreated = $False function Write-Log([string]$logMsg) { if(!$isLogFileCreated){ Write-Host "Creating Log File..." if(!(Test-Path -path $directoryPath)) { Write-Host "Please Provide Proper Log Path" -ForegroundColor Red } else { $script:isLogFileCreated = $True Write-Host "Log File ($logFileName) Created..." [string]$logMessage = [System.String]::Format("[$(Get-Date)] - {0}", $logMsg) Add-Content -Path $logPath -Value $logMessage } } else { [string]$logMessage = [System.String]::Format("[$(Get-Date)] - {0}", $logMsg) Add-Content -Path $logPath -Value $logMessage } } #######The below function will clean up the items from SharePoint Recyle Bins.############################ function CleanUpRecylceBin() { param ( [Parameter(Mandatory=$true)] [string] $WebAppURL ) #Web Application URL $myWebApp=Get-SPWebApplication $WebAppURL #Looping thru the collection of site collections from the given web application. foreach ($oneSPSite in $myWebApp.Sites) { #Looping thru the collection of webs from the each site collection. foreach($oneSPWeb in $oneSPSite.AllWebs) { #The below code will empty the first Stage Recycle bin items permanently. #$oneSPWeb.RecycleBin.DeleteAll(); #The below code will send the first Stage Recycle bin items to second Stage Recycle bin. $oneSPWeb.RecycleBin.MoveAllToSecondStage(); Write-Host "The first stage (End-User) Recycle Bin items have been Deleted successfully for: "$oneSPWeb.title ":" $oneSPWeb.URL "`n" #Write-Host $oneSPWeb.title ":" $oneSPWeb.URL "`n" #Dispose web object $oneSPWeb.Dispose() } #The below code will empty SharePoint site collection recycle bin (Second Stage Recycle bin) or also known as Admin Recycle bin $oneSPSite.RecycleBin.DeleteAll(); #Dispose site object $oneSPSite.Dispose() write-host "The second stage (Admin Recycle Bin) Recycle Bin items have been Deleted successfully for:" $oneSPSite.RootWeb.Title "`n" } } ####################Testing - calling the function######################################################## try { CleanUpRecylceBin "http://yourwebapplicationURL" $message="The CleanUpRecylceBin script execution has been completed successfully." Write-Host $message -BackgroundColor Green } catch { $ErrorMessage = $_.Exception.Message +"in the CleanUpRecylceBin execution script!: " Write-Host $ErrorMessage -BackgroundColor Red Write-Log $ErrorMessage } ####################Testing - calling the function ends here####################################
Summary: SharePoint Recycle Bin
Thus, in this article, we have learned about how to remove items from SharePoint Recycle Bin using the PowerShell script, we can summarize this article as below:
- What is Recycle Bin in SharePoint?
- Using PowerShell how to delete first-stage recycle bin
- Using PowerShell how to delete second stage recycle bin
- How long are deleted items kept in the Recycle Bin?
See Also: SharePoint Recycle Bin
You may also like the following SharePoint PowerShell tutorials:
- [Verified]: Cancel SharePoint workflows using PowerShell
- SharePoint Online: Remove custom app using PnP PowerShell
- Office 365: How to create content type in SharePoint Online using PowerShell?
- Office 365: How to create document library in SharePoint Online using PowerShell?
- Export SharePoint user information list to CSV(Excel) file using PowerShell
- How to fix “The term ‘Get-MsolUser’ is not recognized as the name of a cmdlet”
- How to fix the “The term ‘Get-SPWeb’ is not recognized as the name of a cmdlet, function” PowerShell error
- How to hide quick launch menu in SharePoint online using PnP PowerShell
- Edit user Permission is greyed Out SharePoint Online
- Get workflow inventory from SharePoint online using PowerShell CSOM
- Create a modern team site using PnP PowerShell in SharePoint
- In 2 steps convert a classic SharePoint page to modern using PnP
- SharePoint Online: Delete All Files from document library for the given date – PowerShell CSOM
- Create SharePoint online list using PnP provisioning template
- SharePoint Automation: PowerShell script to get remote server information
- Office 365: Retrieve hub sites and associated sites using PnP Powershell
- SharePoint Online Automation – O365 – Upload files to document library using PowerShell CSOM
- SharePoint Online Automation – O365 – Create multiple items in a list using PowerShell CSOM
- SharePoint Online Automation – O365 – Update document library metadata using PowerShell CSOM
- Introduction to SharePoint information architecture
1 comments on “SharePoint Recycle Bin: Quickly delete recycle bin items in SharePoint using PowerShell”