Cleaning up deleted databases in SharePoint

Problem

A service application database was deleted in SQL Server Management Studio. After a while, I noticed errors in the event viewer on the WFE and ULS logs complaining that SharePoint was not able to login to the database that I deleted. Obviously, SharePoint still thought that the database existed so I needed to find a way to remove it.

PowerShell to the rescue.

Solution

Using PowerShell, run the following command:

Get-SPDatabase | where {$_.exists -eq $false}

This will list all databases in SharePoint that no longer exist on the database server. If you are happy with the result and wish to remove the orphaned databases, run the following command:

Get-SPDatabase | where {$_.exists -eq $false} | foreach {$_.delete()}

All orphaned databases should now be removed and SharePoint should stop complaining about being unable to login to the non-existent database.

Tags: ,

4 Responses to “Cleaning up deleted databases in SharePoint”

  1. Daniele January 13, 2014 at 11:28 pm #

    Very appreciated, thank you 🙂

  2. Nick July 25, 2014 at 11:08 pm #

    Great tip – thanks a lot!

  3. Barbara Joost October 20, 2015 at 6:52 pm #

    Some thinks can be so easy. Thank you.

  4. Kelly November 2, 2015 at 11:23 pm #

    Much appreciated — thanks!!

Leave a Reply

%d bloggers like this: