Remove a Feature from a content DB

This power shell code provide the option to remove missing features referenced on a content database migration log.

Before migrating content Database you need to test the DB using the test-spcontentdatabase powershell command. If you encountered the error message bellow :

[MissingFeature] Database [WSS_Content] has reference(s) to a missing feature: Id = [XXXXXX-XXXXXX-XXXXXX-XXXX-xxxxxxxx]. Feature (Id = [XXXXXX-XXXXXX-XXXXXX-XXXX-xxxxxxxx]) is referenced in database [WSS_Content], but isn't installed on the current farm. The missing feature might cause upgrade to fail. If necessary, please install any solution that contains the feature and restart upgrade.

You can use this code on your discretion ( backup first)

function Remove-SPFeatureFromContentDB($ContentDb, $FeatureId, [switch]$ReportOnly){
    $db = Get-SPDatabase | where { $_.Name -eq $ContentDb }
    [bool]$report = $false
     if ($ReportOnly) { $report = $true }
     $db.Sites | ForEach-Object {
          Remove-SPFeature -obj $_ -objName "site collection" -featId $FeatureId -report $report
         $_ | Get-SPWeb -Limit all | ForEach-Object {
            Remove-SPFeature -obj $_ -objName "site" -featId $FeatureId -report $report
          }
    }
}

function Remove-SPFeature($obj, $objName, $featId, [bool]$report){
  $feature = $obj.Features[$featId]
   if ($feature -ne $null) {
     if ($report) {
      write-host "Feature found in" $objName ":" $obj.Url -foregroundcolor Red
  }
   else{
      try {
        $obj.Features.Remove($feature.DefinitionId, $true)
         write-host "Feature successfully removed from" $objName ":" $obj.Url -foregroundcolor Red
     }
      catch {
     write-host "There has been an error trying to remove the feature:" $_
    }
  }
 }
else {
#write-host "Feature ID specified does not exist in" $objName ":" $obj.Url
}
}
Remove-SPFeatureFromContentDB -ContentDB "WSS_Content_RecallsTest" -FeatureId "XXXX_XXXXXX_XXXXX_XXXX" 

ref :

Removing features from a content database in SharePoint 2010 using PowerShell

Leave a Reply

Your email address will not be published. Required fields are marked *