SharePoint 2013: Get-SPWeb : Cannot find an SPWeb object with Id or Url: and site Url [URL]

Problem

You are resolving MissingFeature missing dependencies surfaced by test-spContentDatabase for a single site collection hosted in a content database mounted to a development 2013 farm used for upgrading.  You've executed this bit of SQL against the content database to get a listing of the feature instances associated with the missingfeature dependency:

USE SP13_Content_Contoso
SELECT SiteID,WebID,FeatureID
FROM Features WITH (NoLOCK)
WHERE FeatureID IN
  ('50e31660-5eaa-4691-9f44-04552b127ad5',
   'ad739f9e-1525-4dec-a25e-10821ca70c95',
   .
   .
   .
   '90014905-433f-4a06-8a61-fd153a27a2b5')

You then develop your PowerShell statements from this data to remove the features from the site collection or web, as appropriate.  Most of the statements complete without error, but a few return an error similar to the following:

Get-SPWeb : Cannot find an SPWeb object with Id or Url:  and site Url http://FarmCA:1234/sites/site1.
At line:1 char:2
+ (Get-SPWeb -Identity "A3A60612-1A15-4B6F-8752-E45C30B6CCD4" -Site "A12AC13D-DD28 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (Microsoft.Share....SPCmdletGetWeb:SPCmdletGetWeb) [Get-SPWeb], SPCmdletPip
   eBindException
    + FullyQualifiedErrorId : Microsoft.SharePoint.PowerShell.SPCmdletGetWeb

You cannot call a method on a null-valued expression.
At line:1 char:1
+ (Get-SPWeb -Identity "A3A60612-1A15-4B6F-8752-E45C30B6CCD4" -Site "A12AC13D-DD28 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You re-execute the SQL statement and find that most of the items no longer appear except for those corresponding to the PowerShell statements that failed.

Solution

The clue lies in that first line of the error statement (highlighted):

Get-SPWeb : Cannot find an SPWeb object with Id or Url:  and site Url http://FarmCA:1234/sites/site1.

Note that no URL is provided for the SPWeb object, but a URL is provided for the site collection.  This is because the web is not present in the site collection's spweb collection but it is present in the site collection's RecycleBin collection.  Because it still actually exists.  It was deleted from the collection of webs and is now contained within the collection of deleted items.  Though deleted, there is still a reference to it in the hosting content database's Features table.

The solution then is simple: just flush the Recycle Bin - both just to be sure and if you are in a hurry.  Follow this up with a fresh re-execution of the SQL script to verify that feature has been removed from the features table.

NOTE: if the missingfeature dependency still surfaces in the test-spcontentdatabase report, even though it no longer appears in the Features table after you removed it using the above steps, then that particular feature ID is likely associated with a list or document library that has been created in the web when that feature was first activated. It will take a different approach to identify which lists or libraries are associated with that feature ID and then to remove those lists or libraries.

References