2

Running version Sitecore 9.3 - and the latest version of the Powershell package

The Powershell documentation has an example of deleting an individual item.

$database = Get-Database -Name "master"
$archiveName = "recyclebin"
$archive = Get-Archive -Database $database -Name $archiveName
Remove-ArchiveItem -Archive $archive -ItemId "{1BB32980-66B4-4ADA-9170-10A9D3336613}"

Is it possible to have the Remove-archive item iterate through the whole list of archived items, but only delete items older than 1 year?

*** Edit I managed to put a solution together prior to both of the Sumits who posted, but their solutions are cleaner.

function GetRecycleBinItems {

$database = Get-Database -Name "master"
$archiveName = "recyclebin"
$archive = Get-Archive -Database $database -Name $archiveName
Get-ArchiveItem -Archive $archive
}

$limit = [datetime]::Now.AddDays(-365)

$items = GetRecycleBinItems
foreach($item in $items) {
    if ($item.ArchiveDate -lt $limit) {
        Remove-ArchiveItem -Archive $archive -ItemId $item.ItemId
    }
}
1

2 Answers 2

0

Here is the official documentation given by Sitecore where you can find the examples to iterate through the archive items. Hope this document link will work for others to get multiple examples of PowerShell scripts. Here is the link.

https://doc.sitecorepowershell.com/code-snippets

You will find lots of code snippets and the code you are looking for is under the Restore Recycle bin items section. That is used to restore the items. But in your case you want to delete those items then you can modify your code a little bit.

[datetime]$archivedDate = [datetime]::Today.AddDays(-365)  #Add the days

foreach($archive in Get-Archive -Name "recyclebin") {
    $entries = $archive.GetEntries(0, $archive.GetEntryCount())
    foreach($entry in $entries) {
        if($entry.ArchiveLocalDate -ge $archivedDate) { 
             $archive.RemoveEntries($entry.ArchivalId)
        } 
    }
}

Hope this helps.

2
  • Thank you, I managed to put together a solution late yesterday afternoon but this is a much cleaner loop.
    – eKoz
    Jun 8, 2023 at 12:51
  • 3
    Technically the documentation is by me, not Sitecore. Jun 8, 2023 at 15:06
1

Yes, you can delete entries from recycle bin based on any date by passing the days value in the below script. As it sets to -356 days to delete only items older than 1 year. You can change the day value according to your need to change the date value.

[datetime]$archivedDate = [datetime]::Today.AddDays(-365)
Write-Host "Delete items older then $($archivedDate.ToShortDateString())"

foreach($archive in Get-Archive -Name "recyclebin") {
    Write-Host " - Found $($archive.GetEntryCount()) entries"
    $entries = $archive.GetEntries(0, $archive.GetEntryCount())
    foreach($entry in $entries) {
        if($entry.ArchiveLocalDate -ge $archivedDate) { 
            Write-Host "Delete item: $($entry.OriginalLocation) {$($entry.ArchivalId)} on date $($entry.ArchiveLocalDate)"
            $archive.RemoveEntries($entry.ArchivalId)
        } else {
            Write-Host "Skipping $($entry.OriginalLocation) on date $($entry.ArchiveLocalDate)"
        }
    }
}

Hope it helps!

1
  • Thank you, this is a perfect example. If my rep was higher I would vote both of the answers up for how well they were written
    – eKoz
    Jun 8, 2023 at 12:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.