top of page

Manage Sitecore item versions using power shell

  • Writer: Sridharan Padmanabhan
    Sridharan Padmanabhan
  • Jan 19, 2024
  • 2 min read

Updated: Dec 13, 2024


This Power Shell script helps you manage and recycle old Sitecore item versions without having to build or utilize any additional tools/custom code.


Sitecore recommends having not more than 10 versions on an item for optimal performance. A huge number of versions on multiple items across the content tree would have a significant negative impact on the content authoring performance.



The script takes in the following parameters -

  1. Branch : to use as the parent node and recurse through.

  2. Number of versions to keep : versions older than this number would be considered for recycling.

  3. Language: The language to audit.

  4. Delete Older versions? : A check box to choose whether to delete. Leaving this check box unchecked would just show a report of versions that are older than the selected number.

$item = Get-Item -Path "/sitecore/content/myWebsite"

$sysLanguages = Get-Item -Path "/sitecore/system/Languages"

$dialogProps = @{
 Parameters = @(
 @{ Name = "item"; Title="Select branch"; Root="/sitecore/content/DH/Hospitality/Jumeirah/"}, @{ Name = "count"; Value=5; Title="Number of versions to keep"; Editor="number"},
 @{ Name = "sysLanguages"; Title="Select Language"; Root="/sitecore/system/Languages/"}, @{ Name = "remove"; Value=$False; Title="Delete older versions?"; Editor="check"})

Title = "Check the 'Delete older versions' checkbox to delete the versions, unchecking it only gives a report of older versions"

Description = "Fill in the parameters below"

Width = "1200"

OkButtonName = "Proceed"

CancelButtonName = "Abort"}


$result = Read-Variable @dialogProps

if($result -ne "ok") { Close-Window

Exit}
$itemVersions = Get-ChildItem -Path $item.Paths.FullPath -Language $sysLanguages.Name -recurse -Version * | Where-Object {$_.Versions.Count -gt $count}
$recycleVersions = @()
foreach($itemVersion in $itemVersions)
{
    $masterLatestVersionNumber = $itemVersion.Versions.GetLatestVersion().Version.Number
    $webItemPath = "web:" + $itemVersion.Paths.FullPath
    $webitem = Get-Item -Path $webItemPath -Language $sysLanguages.Name -ErrorAction SilentlyContinue -ErrorVariable myError

if ($myError.Count -gt 0 -and !($myError[0].FullyQualifiedErrorId.Contains("ItemDoesNotExist")))         {
            $recycleCountMaster = $masterLatestVersionNumber - $count
            if($itemVersion.Version.Number -le $recycleCountMaster)
            {
            $recycleVersions += @($itemVersion) | Where-Object {$itemVersion.Version.Number -le $recycleCountMaster}
            }
        }
    else
    {
        $webItemVer = $webitem.Version.Number
        $recycleCountWeb = $webItemVer - $count
        if($itemVersion.Version.Number -le $recycleCountWeb){
            $recycleVersions += @($itemVersion) | Where-Object {$itemVersion.Version.Number -le $recycleCountWeb}
        }
    }
}

if($remove)
{
foreach($recycleVersion in $recycleVersions)
{
    Remove-ItemVersion $recycleVersion
    Write-Host "Recycled: ",$recycleVersion.Paths.FullPath","$recycleVersion.Language.Name", Current: "$recycleVersion.Version", Latest: "$recycleVersion.Versions.GetLatestVersion().Version.Number
}

}
else
{
    $recycleVersions | Show-ListView
}

Comments


Sitecore with Sri

Sridharan Padmanabhan

  • Instagram
  • LinkedIn
bottom of page