Unpublish a language from a Sitecore website

Created: 6 Dec 2023, last update: 3 Oct 2024

Tip: Unpublish a language from a Sitecore SXA website

When you need to eliminate a specific language from a multilingual Sitecore website, the process involves removing all language versions. Sitecore PowerShell can be employed to achieve this. See Remove a language from an SXA site. Understandably, the content editor who invested considerable effort into creating the content may prefer to retain it for a certain period. In such cases, consider unpublishing all versions of the items associated with the particular language.

Unpublish can be done with the publish restrictions, uncheck the 'Publishable' checkbox for the version. In code, this corresponds to the '__Hide version' field. After applying the publish restrictions remember that you need to publish in order to unpublish. It's important to note that the actual publishing step is not included in the PowerShell script provided below.

Script for unpublishing an item and all its children:

$startPath =  "master:/sitecore/content/mytentant/mysites/mysite/Home"
$language = "nl"

$items = Get-ChildItem -Path $startPath -Recurse
$rootItem = Get-Item -Path $startPath
$items = $items + $rootItem

foreach ($item in $items)
{
    foreach ($version in $item.Versions.GetVersions($true))
    {
        if ($version.Language -eq $language)
        {
            #Write-Host $version.Name $version.Language $version.Version.Number "- found   hide=" $version.Fields["__Hide version"]
            if ($version.Fields["__Hide version"].value -ne "1")
            {
                $version.Editing.BeginEdit()
                $version.Fields["__Hide version"].Value = "1"
                $version.Editing.EndEdit()
                Write-Host $version.Name $version.Language $version.Version.Number "- set as not Publishable"
            }
        }
    }   
}