SharePoint 2007: How to Delete an Orphan from Configuration Database

We attempt to attach a content database using below command

STSADM.EXE -o addcontentdb -url  http://sush07 -databasename WSS_Content –databaseserver Peaches


The attach operation cannot continue because another object in this farm already contains the same ID. Each object in a farm must have a unique ID. In order to proceed with the attach operation you must assign a new ID to this database. To attach this database with a new ID, use the “stsadm.exe —o addcontentdb” operation with the —assignnewdatabaseid parameter. Note that if this new database and an existing database contain the same site collections, attaching this database will likely result in orphaned site collections due to conflicts between the two databases.

Now attempt to attach the database using -assignnewdatabaseid parameter

STSADM.EXE -o addcontentdb -url http://sush07 -databasename Wss_Content –databaseserver Peaches -assignnewdatabaseid


A SharePoint database named  already exists. You must supply another name for the new database.

Step 1:

As the above error indicates that a database already exists to confirm, log on SQL Server and open SQL Server Management Studio and execute the below query against the SharePoint Configuration Database.

select Name, ID from Objects where Properties like '<object type="Microsoft.SharePoint.Administration.SPContentDatabase%'

After executing the above query, search for the database and it would be listed.

We can use stsadm –o deleteconfigurationobject however, executing it on production farm isn’t supported until prior approval from Microsoft.


Step 2:

Install PowerShell in Windows 2003

PowerShell on MOSS 2007

Install : (No Restart Required)

Run PowerShell and then execute: Set-ExecutionPolicy UnRestricted

Press Y

Before programming against the SharePoint and/or MOSS 2007 object model the appropriate assemblies must be loaded. The following commands do this:

[System.Reflection.Assembly]::Load(“Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c”)

[System.Reflection.Assembly]::Load(“Microsoft.SharePoint.Portal, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c”)

Step 3:

Copy the below code in notepad and save it as orphan.ps1 and save it on C:\


$farm = [microsoft.sharepoint.administration.spfarm]::Local

$dbservice = $farm.Services | where { $_.typename -eq 'Windows SharePoint Services Database' }

$instance = $dbservice.Instances | select -First 1

$dbs = $instance.Databases

$Property = [Microsoft.SharePoint.Administration.SPContentDatabase].GetProperty("DisplayName")

foreach ( $db in $dbs )


    $Property.GetValue($db, $null)


To execute the above script type . .\orphans.ps1 within PowerShell this would listed all the databases.

Step 4:

Copy the below code in notepad and save it as DeleteOrphan.ps1 and save it on C:\

Note: Change the name of the database

$DbToDelete = "WSS_Content"

foreach ( $db in $dbs )


    $value = $Property.GetValue($db, $null)

     if ( $value -eq $DbToDelete) { $targetdb = $db }


$delete = [Microsoft.SharePoint.Administration.SPContentDatabase].GetMembers() | Where { $ -eq "delete" }

$delete.Invoke($targetdb, $null)

To execute the above script type . .\deleteorphan.ps1 within PowerShell this would delete the reference of the specified databases from configuration database.

Re-execute the SQL query as per Step 1 and confirm that we don’t see the database listed.

Later, try to attach the database

STSADM.EXE -o addcontentdb -url http://sush07 -databasename WSS_Content –databaseserver Peaches


Operation Completed Successfully.






Comments (4)

  1. Anonymous says:

    Excellent artcle.. bookmarked 🙂 Thanks

  2. BlueSky2010 says:

    Nevermind – found the solution to remove the orphaned entry from the Config DB. In case this helps someone else:

    $orphanedDB = Get-SPDatabase | where{$_.Name -eq "MyContentDatabase"}


  3. Anonymous says:

    perfect one 🙂

  4. BlueSky2010 says:

    Hi Sushant,

    While trying to execute Orphans.ps1 from your step 3 I'm coming across the following error – do you have any   suggestions for me?

    I'm trying this on a SharePoint 2010 farm with SP1 applied.

    Many thanks in advanced!

    Exception calling "GetValue" with "2" argument(s): "Non-static method requires

    a target."

    At D:Deployedorphans.ps1:19 char:27

    +         $Property.GetValue <<<< ($db, $null)

       + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

       + FullyQualifiedErrorId : DotNetMethodException

Skip to main content