Remove all Sitecore Language Versions Except One Using Powershell / Blogs / Perficient


In the Sitecore CMS, we can add new versions of any item, and we can add as many versions as needed. There are 2 types of versions available in Sitecore

  1. Numbered version – versions of items in the same language
  2. Language version – versions of an item in a different language

Problem

In some cases, we may want to remove versions of an item or an entire node in all languages except one or a few languages. In that case, it is difficult to remove them manually, there are only a few options to tackle this challenge: either remove language versions of items one by one or use PowerShell script in Sitecore.

The Sitecore Powershell Extension is a module that provides a command-line interface and scripting environment to work with the Sitecore platform. Using the Powershell Extension, we can perform various operations of Sitecore items, as well as allow access to Sitecore APIs. It looks and works in the same way as the Windows Powershell utility.

The following is the essential powershell parameter that we are using:

Remove-ItemVersion

Removes Language/Version from a single item or a branch of items Removes Language/Version from a single item or a branch of items.

 

  • Remove-ItemVersion -Language <String[]> [-Version <String[]>] [-ExcludeLanguage <String[]>] [-Path] <String> [-Recurse] [-MaxRecentVersions <Int32>]
    Remove-ItemVersion -Language <String[]> [-Version <String[]>] [-ExcludeLanguage <String[]>] -Id <String> [-Database <String>] [-Recurse] [-MaxRecentVersions <Int32>]
    Remove-ItemVersion [-Language <String[]>] [-Version <String[]>] [-ExcludeLanguage <String[]>] [-Item] <Item> [-Recurse] [-MaxRecentVersions <Int32>]

     

Solution

Follow the steps below to resolve the problem. Here is my example to remove all other languages except English:

  1. Retrieve the path of the item on which we want to perform an action
  2. Retrieve the language version code that you want:
    1. To retrieve language code, go to “/sitecore/system/Languages”
    2. Go to a specific language and take value from the field “Regional Iso Code”
  3. Go to Powershell ISE from Sitecore Launchpad
  4. Add PowerShell script mentioned below and update the $sourcepath variable and language code accordingly

Sitecore PowerShell Script

$props = @{
   InfoTitle = "Remove Versions"
    PageSize = 10000000
}
 
$sourcePath =  "{Please Add Content Path Here}"
 
function Remove-Versions {
 
    $items = Get-ChildItem -Path $sourcePath -Recurse
    $rootItem = Get-Item -Path $sourcePath
    $items = $items + $rootItem
 
    foreach ($item in $items)
    {
        foreach ($version in $item.Versions.GetVersions($true))
        {
            if ($version.Language -ne "{Please Add Language Code}")
            {
                Remove-ItemVersion $version
                Write-Host $version.ID " - " $version.Language "- deleted"
                $version;
            }
        }   
    }
}

$items = Remove-Versions
$items | Show-ListView @props -Property ItemPath, ID, @{Label="Language"; Expression={$_."Language"}} 
Close-Window

 

Let’s See How It Works

Remove All Languages Except One using powershell

 

We hope this helps! Check out our Sitecore blog for more helpful tips and tricks.





Source link

Social media & sharing icons powered by UltimatelySocial
error

Enjoy Our Website? Please share :) Thank you!