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

- 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

- Click on Powershell Scripted Task Schedule following the below image

- Select the previously selected script. You can also choose multiple scripts at a time. It executes based on the order of selection.

- Scheduling script on specific time and day.

- Finally Scheduled task looks like this.You can make it run async by checking the Async checkbox

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.

Very cool. I used a query to get items based on template, but it does not look like Remove-ItemVersion goes through the list. Only one path at a time. May need a foreach if that is the case.
LikeLike
@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 🙂 .
LikeLike