Removing old versions using Sitecore Scheduled Tasks

2 comments

We need scheduled tasks to achieve a lot of our day today activities. This article describes how we can quickly create a scheduled task using Sitecore Powershell extension module.

We had several jobs running in our Sitecore instance that would update Sitecore items based on contents from different platforms (ex: Salesforce, Eloqua , HubSpot etc). These jobs might run everyday or during specific day of the week. Each time the Job finishes running, it would create a new version of an item.
(In our case we had a new version created during every update. However, this feature can be disabled by setting RequireLockBeforeEditing to false <setting name=”RequireLockBeforeEditing” value=”false”  /> )

In–order to clean up the old version, I implemented a simple scheduled task.

Let’s see in detail how it is implemented using Powerful Sitecore Powershell Extension Module

  • Creating Script to clean up Old versions – Open Powershell ISE
Sitecore SPE
Image 1 : Open Powershell Extension
  • Fill in the below script
New-UsingBlock(New-Object Sitecore.Data.Events.EventDisabler) {
    #Path of items-update below
    $query = 'master:/sitecore/content/custompath'
    #remove versions for specific language or all languages
    Remove-ItemVersion -Path $query -Language "en" -Recurse -MaxRecentVersions 1
}

  • Save the script under a specific Powershell Script directory
Save-Sitecore powershell script
Image 2 : Save the script under a folder
  • Click on Powershell Scripted Task Schedule following the below image
Powershell Scripted Task Schedule
Image 3 : Powershell Scripted Task Schedule
  • Select the previously selected script. You can also choose multiple scripts at a time. It executes based on the order of selection.
Select Script
Image 4 : Removing Old Version Selection
  • Scheduling script on specific time and day.
Schedule SPE script
Image 5 : Scheduling script based on specific time and date
  • Finally Scheduled task looks like this.You can make it run async by checking the Async checkbox
SPE Schedule task
Image 6 : Scheduled tasks

This was really easy thing to do and it works remarkably well !!

Every time it finishes running, you would see the details in Last run date time field.

2 comments on “Removing old versions using Sitecore Scheduled Tasks”

    1. @reilleyweb – Yes that’s true, you need to loop in the items as Remove-ItemVersion takes one path at a time. It also has an option to Recurse within a specific path – might be useful while working on child item under a specific path 🙂 .

      Like

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 )

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.