Detect and Repair Content DB orphans / Security Corruption (Missing ScopeID) in a SharePoint 2010 / 2013 Farm using Windows PowerShell (Updated!)

We have come across issues like unable to access / unable to delete / unable to change permissions / unable to access version history for a document or library / list due to Content DB orphans OR security corruption (Missing ScopeID). You may see error similar to below in ULS logs.

<Date Time> w3wp.exe (PID) <TID> SharePoint Foundation General 8kh7 High The URL 'DocumentLibrary/DocumentName.docx' is invalid. It may refer to a nonexistent file or folder, or refer to a valid file or folder that is not in the current Web. <Correlation ID>

<Date Time> w3wp.exe (PID) <TID> SharePoint Foundation General 8nca Verbose Application error when access /_layouts/Versions.aspx, Error=The URL 'DocumentLibrary/DocumentName.docx' is invalid. It may refer to a nonexistent file or folder, or refer to a valid file or folder that is not in the current Web. at Microsoft.SharePoint.Library.SPRequestInternalClass.GetFileVersions(String bstrWebUrl, String bstrFileUrl, Byte level, Object& pvarVersions, UInt32& pdwcVersions) at Microsoft.SharePoint.Library.SPRequest.GetFileVersions(String bstrWebUrl, String bstrFileUrl, Byte level, Object& pvarVersions, UInt32& pdwcVersions) <Correlation ID>

<Date Time> w3wp.exe (PID) <TID> SharePoint Foundation Runtime tkau Unexpected System.Runtime.InteropServices.COMException: The URL 'DocumentLibrary/DocumentName.docx' is invalid. It may refer to a nonexistent file or folder, or refer to a valid file or folder that is not in the current Web. at Microsoft.SharePoint.Library.SPRequestInternalClass.GetFileVersions(String bstrWebUrl, String bstrFileUrl, Byte level, Object& pvarVersions, UInt32& pdwcVersions) at Microsoft.SharePoint.Library.SPRequest.GetFileVersions(String bstrWebUrl, String bstrFileUrl, Byte level, Object& pvarVersions, UInt32& pdwcVersions) <Correlation ID>

Good thing is, Content DB orphans / Security Corruption (Missing ScopeID) could be detected using stsadm -o databaserepair or using a PowerShell script such as below for SharePoint 2010 /2013 Farm.

Note:

  1. Please take farm / Content DB backup before you attempt to use this script. I or Microsoft are not responsible for any damages due to wrong usage of the script!
  2. Script runs at Farm level, enumerates thru each Content DB so script could take some time to complete if there are many Content DBs / large farm.
  3. Script is Read-Only (just detects orphans – if any).
  • IMPORTANT: Script can be changed to remove orphans by setting bool DeleteCorruption to $true. I strongly suggest to use this script initially only for detection and enable DeleteCorruption flag only after ensuring working backup of affected Content DBs / entire farm (if required).
 if ((Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) 
 {Add-PSSnapin Microsoft.SharePoint.Powershell}
  
 $CDBs = Get-SPContentDatabase
 ForEach ($CDB in $CDBs)
 {
     Write-Host "Detecting Orphans for " $CDB.Name
     $CDB.Repair($false)
 }