Sitecore delete old item versions

Are your Sitecore editors version-trigger-happy? Is your master database items drowning in old versions that will never be used – or is just exact replicas of the previous version?

Many versions of the same item

Many versions of the same item

Fear not, you can in fact – with a little bit of code of course – recycle versions of items.

This tiny class will recursively run through items from the root of your choice, and recycle versions, except those you wish to keep:

namespace MyCode
  private class Cleanup
    public void CleanUpItemVersions(Item item, int maxVersionCount)
      int latestVersionNumber = item.Versions.GetLatestVersion().Version.Number;
      if (latestVersionNumber > maxVersionCount)
        int latestVersionToKeep = latestVersionNumber - maxVersionCount;
        RecycleVersions(item, latestVersionToKeep);

      foreach (Item child in item.Children)
        CleanUpItemVersions(child, maxVersionCount);

    private static void RecycleVersions(Item item, int latestVersionToKeep)
      Item[] versions = item.Versions.GetVersions();
      foreach (Item version in versions)
        if (version.Version.Number <= latestVersionToKeep)


This method will clean all but the latest 3 version from all english items below /sitecore/content/home:

publicvoid CleanUpItemVersions()
  Database master = Database.GetDatabase("master");
  Item rootItem = master.GetItem("(sitecore/content/home", LanguageManager.GetLanguage("en"));
  var cleanup = new Cleanup();
  cleanup.CleanUpItemVersions(rootItem, 3);



About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in .net, c#, General .NET, Sitecore 5, Sitecore 6, Sitecore 7, Sitecore 8, Sitecore 9 and tagged , , . Bookmark the permalink.

3 Responses to Sitecore delete old item versions

  1. mikaelnet says:

    Thanks for sharing! Inflation of version numbers are a common issue. However, there may be inbound links to specific versions. Any thoughts on that? Links in the __Source field (clones) typically point out a version, and if that wouldn’t be good to delete. Item version numbers are also included in for example Notifications. Any thoughts on how to ensure nothing gets destroyed when deleting old versions?

  2. mikaelnet says:

    Btw, one may need to deal with lifetime and workflow states as well to ensure the current active/publishable version isn’t recycled.

  3. JCG says:

    Thanks Brian, great post. Any idea how to limit the number of item versions in XP 9 to avoid this from happening in the future?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.