Sitecore - efficiently unlock all items with Powershell

Posted 11 Oct 2023 by Marek Musielak

effectively unlock all the items with sitecore powershell extensions

Below you can find Sitecore Powershell Extensions script that will efficiently unlock all Sitecore items.

Why it's more efficient than other scripts?
It uses sitecore_master_index to find all the lock items and calls Unlock method only when necessary. It unlocks all the locked versions in all languages, not only the latest version.

What is the script output?
Output contains ID, language, version and path of unlocked items. Additionally you may see messages "indexed version contains incorrect lock information" or "in index but does not exit in database".

Code of the script:

$criteria = @(
    @{Filter = "StartsWith"; Field = "_fullpath"; Value = "/sitecore/content" }
    @{Filter = "Equals"; Field = "__lock_s"; Value = ''; Invert=$true }
)

$props = @{
    Index = "sitecore_master_index"
    Criteria = $criteria
}

$lockedItems = Find-Item @props

foreach($lockedItem in $lockedItems){
    $item = Initialize-Item -SearchResultItem $lockedItem
    if ($item -eq $null) {
        echo ($lockedItem.ItemId.ToString() + ' (' + $lockedItem.Language.ToString() + ' v' + $lockedItem.Version.ToString() + ') in index but does not exit in database: ' + $lockedItem.Path)   
    } else {
        $oldRevision = $item['__Revision']

        $item.Locking.Unlock() | Out-null;
        
        $item = Initialize-Item -SearchResultItem $lockedItem
        
        $newRevision = $item['__Revision']
        
        if ($oldRevision -eq $newRevision) {
            echo ($lockedItem.ItemId.ToString() + ' (' + $lockedItem.Language.ToString() + ' v' + $lockedItem.Version.ToString() + ') indexed version contains incorrect lock information: ' + $lockedItem.Path)
        } else {
            echo ($lockedItem.ItemId.ToString() + ' (' + $lockedItem.Language.ToString() + ' v' + $lockedItem.Version.ToString() + ') unlocked: ' + $lockedItem.Path)
        }
    }
}

Comments? Find me on or Sitecore Chat