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)
        {
          version.RecycleVersion();
        }
      }
    }
  }
}

USAGE:

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);
}

MORE TO READ:

 

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.

5 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?

    Like

  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.

    Like

  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?

    Like

  4. Pingback: Sitecore Memory Issues – Every memory optimization trick in the book | Brian Pedersen's Sitecore and .NET Blog

  5. Pingback: Removing Unneeded Sitecore Versions with SPE - Flux Digital Blog

Leave a comment

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