Last active
March 3, 2021 22:21
-
-
Save AdamNaj/1bd3980a9a5dc5014c66 to your computer and use it in GitHub Desktop.
Item Version Pruner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Remove-OldItemVersion { | |
[CmdletBinding()] | |
param( | |
[Parameter(Position = 0,Mandatory = $true,ValueFromPipeline = $true)] | |
[ValidateNotNullOrEmpty()] | |
[Sitecore.Data.Items.Item]$Item, | |
[Parameter(Position = 1,Mandatory = $true)] | |
[ValidateNotNullOrEmpty()] | |
[int]$MaxVersions | |
) | |
process { | |
while ($Item.Versions.Count -gt $MaxVersions) { | |
$Item.Versions[$Item.Versions.GetVersionNumbers()[0]].Versions.RemoveVersion(); | |
} | |
} | |
} | |
# items underneath this path to be pruned | |
$path = "master:\content" | |
#languages can be defined specifically like "en", "pl-PL' or with wildcards like "*" | |
$languages = "*" | |
#Check how many versions are there in each language in each of the items before the trim | |
Get-ChildItem $path -Language $languages -Recurse | ft name,language,@{ Name = "VerCount"; Expression = { $_.Versions.Count } } | |
#ACTION HAPPENS HERE | |
Get-ChildItem $path -Language $languages -Recurse | Remove-OldItemVersions -max 10 | |
#Check how many versions are there in each language in each of the items after the trim | |
Get-ChildItem $path -Language $languages -Recurse | ft name,language,@{ Name = "VerCount"; Expression = { $_.Versions.GetVersionNumbers().Length } } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment