Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This Sitecore PowerShell script removes old versions of items in all languages so that the items only contains a selected number of versions
<#
This script will remove old versions of items in all languages so that the items only contains a selected number of versions.
#>
$item = Get-Item -Path "master:\content"
$dialogProps = @{
Parameters = @(
@{ Name = "item"; Title="Branch to analyse"; Root="/sitecore/content/Home"},
@{ Name = "count"; Value=10; Title="Max number of versions"; Editor="number"},
@{ Name = "remove"; Value=$False; Title="Do you wish to remove items?"; Editor="check"}
)
Title = "Limit item version count"
Description = "Sitecore recommends keeping 10 or fewer versions on any item, but policy may dictate this to be a higher number."
Width = 500
Height = 280
OkButtonName = "Proceed"
CancelButtonName = "Abort"
}
$result = Read-Variable @dialogProps
if($result -ne "ok") {
Close-Window
Exit
}
$items = @()
Get-Item -Path master: -ID $item.ID -Language * | ForEach-Object { $items += @($_) + @(($_.Axes.GetDescendants())) | Where-Object { $_.Versions.Count -gt $count } | Initialize-Item }
$ritems = @()
$items | ForEach-Object {
$webVersion = Get-Item -Path web: -ID $_.ID -Language $_.Language
if ($webVersion) {
$minVersion = $webVersion.Version.Number - $count
$ritems += Get-Item -Path master: -ID $_.ID -Language $_.Language -Version * | Where-Object { $_.Version.Number -le $minVersion }
}
}
if ($remove) {
$toRemove = $ritems.Count
$ritems | ForEach-Object {
$_ | Remove-ItemVersion
}
Show-Alert "Removed $toRemove versions"
} else {
$reportProps = @{
Property = @(
"DisplayName",
@{Name="Version"; Expression={$_.Version}},
@{Name="Path"; Expression={$_.ItemPath}},
@{Name="Language"; Expression={$_.Language}}
)
Title = "Versions proposed to remove"
InfoTitle = "Sitecore recommendation: Limit the number of versions of any item to the fewest possible."
InfoDescription = "The report shows all items that have more than <b>$count versions</b>."
}
$ritems | Show-ListView @reportProps
}
Close-Window
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment