How to remove recycle bin items in SharePoint using PowerShell?

[Verified]: Remove recycle bin items in SharePoint using PowerShell

No comments

 4,674 total views,  14 views today

In this tutorial, we will learn about how to remove items from Recycle Bin in SharePoint using the PowerShell script. By the way, there are two types of Recycle Bin in SharePoint, user-level Recycle Bin (First-Stage Recycle Bin) and Admin Recycle Bin (second-stage Recycle Bin).

What is Recycle Bin in SharePoint?

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 it exceeds the retention time, it is permanently deleted meaning we cannot restore those deleted items.

How long are deleted items kept in the Recycle Bin?

In SharePoint, items are retained for 93 days from the time you delete them from their original location. They stay in the site 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 Recycle Bin exceeds its quota and starts purging the oldest items
  • The items are manually deleted by the site collection administrator from the site collection Recycle Bin

Remove recycle bin items in SharePoint using PowerShell

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:

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:

You may also like the following SharePoint PowerShell tutorials:

Download SharePoint Online & Office 365 Administration eBook

Get the PDF eBook from here:


 
FREE DOWNLOAD

Send download link to:

Subscribe to get exclusive content and recommendations every month. You can unsubscribe anytime.