Sitecore Scheduled task to trim old versions of items

Sitecore Scheduled task to trim old versions of items

By admin January 28, 2024

From our client, we got a requirement to clean up the database, and to keep only 10 versions of each item, first one, and last 9. It happens that they had 300+ versions for most edited items.

The code for the class:

public class TrimOlderVersions
    {
        public bool Enabled { get; set; }
        public int MaxVersions { get; set; }
        public string Database { get; set; }
        public string RootItem { get; set; }

        public void Run()
        {
            if (Enabled)
            {
                Sitecore.Diagnostics.Log.Info("Trimming older versions of items starting", this);
                // Default values.
                MaxVersions = (MaxVersions < 1) ? 10 : MaxVersions;
                Database = (string.IsNullOrWhiteSpace(Database)) ? "master" : Database;

                // Get the database.
                var database = Sitecore.Configuration.Factory.GetDatabase(Database);

                if (database != null)
                {
                    var rootItem = database.GetItem(RootItem);

                    if (rootItem == null)
                    {
                        Sitecore.Diagnostics.Log.Error("Root item not found", this);
                        return;
                    }
                    Iterate(rootItem);
                    Sitecore.Diagnostics.Log.Info("Trimming versions complete", this);
                }
                else
                {
                    // Log.
                    Sitecore.Diagnostics.Log.Warn(string.Format("{0}: Failed to run. Database \"{1}\" is null.", this, Database), this);
                }
            }
            else
            {
                // Log.
                Sitecore.Diagnostics.Log.Info(string.Format("{0}: Task disabled.", this), this);
            }
        }

        protected void Iterate(Item item)
        {
            if (item != null)
            {
                // Get the version count of the item.
                var versionCount = item.Versions.GetVersionNumbers().Length;
                var latestVersion = item.Versions.GetLatestVersion(item.Language).Version.Number;

                // Don't bother looping if they're aren't enough versions.
                if (versionCount > MaxVersions)
                {
                    // Get all the versions we can archive.
                    var versions = item.Versions.GetVersions().Where(i => i.Version.Number <= (latestVersion - MaxVersions + 1) && i.Version.Number!=1);

                    foreach (var version in versions)
                    {
                        var archive = Sitecore.Data.Archiving.ArchiveManager.GetArchive("archive", Sitecore.Configuration.Factory.GetDatabase(Database));
                        if (archive != null)
                            archive.ArchiveVersion(version);
                    }
                }

                // recursive call for children
                foreach (Item child in item.Children)
                {
                    Iterate(child);
                }
            }
        }
    }

and sample config file

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <settings>
      <setting name="TrimVersions.MaxVersions" value="9"/>
      <setting name="TrimVersions.TimeOfDayToRun" value="2:00"/>
    </settings>
  </sitecore>
</configuration>

Dont forget to register the command under Sitecore Tasks - Commands, and create corresponding Scheduler.

Featured Blogs