Introducing the ‘Sitecore Version Pruner’ shared source module!

We’ve been using Sitecore workflow for quite a while now. One of the great things about workflow is that each time a user wants to make a change to a published content item, it automatically creates a new item version for you. The only problem is that eventually, you start really accumulating A LOT of versions. We have some items with 100 or more versions! As discussed on SDN, in order to prevent performance issues it is advisable to delete old/unused versions.

There are several options available as outlined by John West to remove old versions. They each have their pros/cons. The ‘VersionManager’ module has the advantage that it backs up the removed versions before deleting them by serializing the entire item to a XML file. This actually writes ALL versions to the XML file. In order to keep track of which versions were deleted, the file name encodes the versions that were deleted.

We were all set to use VersionManager but ran into a snag.. When a user creates a new version, it attempted to delete the old versions in excess of the currently set maximum. The problem was that the users do not have ‘delete rights’.

This led me to start investigating a different approach. After some lengthy comment exchanging with John West on his post about ‘Remove Old Versions…’, I settled on creating a scheduled task that would include the serialization from VersionManager but would run in the background.

And so I created the ‘Sitecore Version Pruner’ module.

Image

As detailed on the Sitecore Marketplace page, the module..

“is a scheduled task that removes old/unused item versions from your content tree. Removed version(s) may be preserved as serialized XML and/or (BETA) copied to the archive database.”

It basically meets the same requirements as VersionManager except:

  1. It runs as a background task. No need for users to have ‘delete’ rights
  2. It uses the Sitecore Rules Engine to define the “pruning” rules (credit to John West for the idea)
  3. In addition to serialization, it also provides an option to archive the deleted versions to the archive database. Read more about this feature on the about tab here.

I have to give a lot of the credit to John West and Ivan Buzyka (VersionManager module author). I basically took their solutions and combined them.

Happy coding!

Advertisements

About Paul Martin

I enjoy rock climbing, playing guitar, writing code...
This entry was posted in Shared Source, Sitecore, Tasks and tagged , , , , , , . Bookmark the permalink.

11 Responses to Introducing the ‘Sitecore Version Pruner’ shared source module!

  1. Lars nielsen says:

    Nice, very nice
    This is a very clever way of removing old versions, running rules. I love the simple idea, and the fact, that it’s so easy to extend this, or use existing rules (e.g. field check).

  2. Hi Paul,
    we are starting a new Sitecore 7 install to completely redesign our website and wanted to use this module. I see on marketplace this is only tested on 6.5. Does this work on 7 also? Thank you Paul!

    • Paul Martin says:

      Hi Doug,
      I installed it on 7.2 yesterday and it works… Sort of. The one major issue I saw was that the rules for which items/versions to prune are no longer editable due to changes with the rules framework in 7.x. I’ll try looking into this next week.

      • I really appreciate this Paul, it is a great Module and would like to keep using it on the new site!

      • Paul Martin says:

        Hi Doug, I’ve updated the rules configuration to work with the new Sitecore 7.x rule architecture. This will be available as “Sitecore Version Pruner 2.0”. Let me know if you have any problems with it. Thanks!

  3. Hi Paul,
    we would like to run this module in Sitecore XP 8.1
    Have you already tested it on 8.1?
    Can this module delete previous versions for each language?
    Thank you!

    • Paul Martin says:

      Hi Daniel, it should work on 8.1. The module was not written to handle multiple languages, but I believe it could accommodate that without too much effort. Unfortunately I don’t have the time right now to implement. However, if you wrapped this section of code in a loop for each item version, I think you’d get the desired result. If you end up implementing this, please submit back a pull request. Thanks!

  4. Hi Paul,
    I don’t know how to create pull requests. Here is my change:

    protected virtual void ProcessItemTree(Item item)
    {
    if (Sitecore.Context.Job != null)
    {
    Sitecore.Context.Job.Status.Processed++;
    Sitecore.Context.Job.Status.Messages.Add(“processing: ” + item.Paths.Path);
    }

    // Run item against the Item Filter rule(s)
    var ruleContext = new RuleContext();
    ruleContext.Item = item;
    this.ItemFilterRules.Run(ruleContext);

    if (ruleContext.Parameters.ContainsKey(“ItemValidForVersionRemoval”))
    {
    // Rule was passed, so this item’s versions should be trimmed..
    var itemLanuguages = item.Languages.Select(x => Sitecore.Context.ContentDatabase.GetItem(item.ID, x)).ToList();
    itemLanuguages.ForEach(TrimItemVersions);
    }

    // process all descendant items..
    foreach (var child in item.Children.InnerChildren)
    ProcessItemTree(child);
    }

  5. prabhat says:

    Hi Paul,

    Is there a way to delete all version older than last published version in this? Thanks for such a nice work.

    Thanks,
    Prabhat

    • Paul Martin says:

      Hi Prabhat – I guess my first question is: if you only want the last published version why not just stop versioning those items? But to answer your question… yes, I believe you could delete all versions older than the last published version by setting the item filter to “where the number of versions is greater than 1” and the version filter to “where this version number < (current version number – 0)"

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s