Saturday, April 4, 2026

Cleaning Up Sitecore Versions at Scale: A Practical PowerShell Approach

 

Hello Sitecorian Community,

If you’ve worked on a large Sitecore implementation, you’ve probably seen this happen:

Items with 10, 20, sometimes even 50+ versions sitting in the system.

At first, it doesn’t seem like a big deal. But over time:

  • Content trees become heavier
  • Database size increases
  • Performance can degrade
  • Content management becomes harder

And eventually, the question comes up:

How do we safely clean up old versions without breaking anything?

The Real Problem

In enterprise setups (like ours managing hundreds of websites), version sprawl is very common.

Typical scenarios:

  • Frequent content updates create multiple versions
  • Publishing pipelines don’t remove old versions
  • Editors rarely clean up older versions manually
  • Multiple languages multiply the problem

Manually deleting versions is not scalable and also risky.

What We Needed

We wanted a solution that:

  • Works recursively across content trees
  • Supports all languages
  • Keeps only the relevant versions
  • Deletes only safe-to-remove versions
  • Can run in dry-run mode before actual cleanup

Two Cleanup Strategies We Implemented

We ended up building two PowerShell scripts using Sitecore PowerShell Extensions (SPE) depending on the use case.

1️⃣ Basic Cleanup: Keep Latest 3 Versions

This is the simplest and most commonly used approach.

Behavior

For an item with versions:

10, 9, 8, 7, 6

The script will:

  • Keep → 10, 9, 8
  • Delete → 7, 6

If an item has 3 or fewer versions, it is skipped.

When to Use

  • General cleanup
  • Non-workflow-driven environments
  • Quick version reduction across large trees

Core Logic

$allVersions = Get-Item -Path $itemPath -Language $language -Version * |
Sort-Object { [int]$_.Version.Number } -Descending
$versionsToKeep = $allVersions | Select-Object -First 3
$versionsToDelete = $allVersions | Select-Object -Skip 3
foreach ($oldVersion in $versionsToDelete) {
$oldVersion.Versions.RemoveVersion()
}

2️⃣ Smart Cleanup: Keep Latest 3 Approved Versions

This is a workflow-aware cleanup, which is much safer for content-heavy environments.

Behavior

Example:

Versions:
10, 9, 8, 7, 6, 5, 4, 3, 2, 1

Case 1

Approved versions: 10, 9, 8
→ Keep: 10, 9, 8
→ Delete: everything else

Case 2

Approved versions: 10, 9, 7
→ Keep: 10, 9, 7
→ Delete: 8, 6, 5, 4, 3, 2, 1

Case 3

Approved versions: 10, 7
→ Skip (not enough approved versions)

Key Implementation Details

๐Ÿ” Recursive Execution

The script runs across:

  • Root item
  • All children
  • All descendants

๐ŸŒ Multi-language Support

Each language version is processed independently.

๐Ÿงช Dry Run Mode

Always start with:

$dryRun = $true

Then switch to:

$dryRun = $false

GitHub Repository

You can find both scripts here:

It includes:

  • Basic version cleanup script
  • Workflow-based cleanup script
  • Safe dry-run execution

Why This Helped Us

Before this:

  • Version cleanup was manual and inconsistent
  • Content trees became bloated
  • Investigations were harder

After this:

  • Cleanup became automated and safe
  • We reduced unnecessary versions significantly
  • Improved overall content hygiene

When Should You Run This?

  • After large deployments
  • As part of regular maintenance jobs
  • During performance optimization
  • Before database cleanup activities

Reference screenshot:


Final Thoughts

In large Sitecore environments, version management is often overlooked, but it plays a big role in performance and maintainability.

These scripts helped us:

  • Keep content clean
  • Reduce noise in version history
  • Maintain only what actually matters

If you’re dealing with version-heavy content trees, this approach can save a lot of time and effort.

Stay tuned for more Sitecore-related articles, tips, and tricks to enhance your Sitecore experience.

Till then, happy Sitecoring! ๐Ÿ˜Š

Please leave your comments or share this article if it’s useful for you!