SharePoint Recycle Bin, how to remove recycle bin items in SharePoint using PowerShell?

SharePoint Recycle Bin: Quickly delete recycle bin items in SharePoint using PowerShell

One comment

Loading

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:

Download SharePoint Online PDF Book

Download SharePoint Online & Office 365 Administration eBook

Buy the premium version of SharePoint Online & Office 365 administration eBook from here:



Buy SharePoint Online & Office 365 Administration eBook


 

Get the free demo PDF eBook from here:

FREE DOWNLOAD

Send download link to:

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

About Post Author

1 comments on “SharePoint Recycle Bin: Quickly delete recycle bin items in SharePoint using PowerShell”

Do you have a better solution or question on this topic? Please leave a comment