Clear items in Sitecore recycle bin based on their deletion date using PowerShell
- Sridharan Padmanabhan
- Jan 4, 2024
- 1 min read
Updated: Dec 13, 2024
The script permanently removes items from recycle bin that are deleted before / after a certain date.
#Variable used to state that items deleted in the last 90 days will be preserved
[datetime]$deleted90days = [datetime]::Today.AddDays(-90)
Write-Host "Reading items recycled after $($archivedDate.ToShortDateString())"
$countskip = 0
$countdelete = 0
foreach($archive in Get-Archive -Name "recyclebin") {
Write-Host " - Found $($archive.GetEntryCount()) entries"
$entries = $archive.GetEntries(0, $archive.GetEntryCount())
foreach($entry in $entries) {
#Loop skips items deleted in the last 90 days
if($entry.ArchiveLocalDate -ge $deleted90days) {
Write-Host "Skipping item: $($entry.OriginalLocation) deleted on $($entry.ArchiveLocalDate)"
$countskip = $countskip + 1
}
#Loop to permanently delete items deleted earlier than 90 days
else {
Write-Host "Permanently deleting $($entry.OriginalLocation) deleted on $($entry.ArchiveLocalDate)"
$archive.RemoveEntries($entry.ArchivalId) #Action that permanently deletes the items
$countdelete = $countdelete + 1
}
}
}
Write-Host "Number of Items deleted: ", $countdelete , " and Number of Items skipped: ", $countskip
The parameter $deleted90days can be used to control the date before which is to be considered.
The script takes a good amount of time to complete, about 5 minutes for every 1000 items.
Comments