Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete (localdb) database if the file is gone

If I run SQL Server Management Studio, and tell it to connect to (localdb)\v11.0, it knows about every database I've ever used, despite the fact that most of the the database files are long gone.

If I ask it to delete one of these databases, it complains that it can't DROP the database because the database file is gone (duhhh). So, how do I clean up this mess and delete all of database references whose assicated database files are gone?

Bob

like image 418
Bob.at.Indigo.Health Avatar asked Nov 16 '12 05:11

Bob.at.Indigo.Health


People also ask

How do I clear my LocalDB database?

In order to be deleted, first it should be stopped. Type the SqlLocalDB stop MSSQLLocaDB command in the Command Prompt window: LocalDB instance “MSSQLLocalDB” stopped. Now, repeat the SqlLocalDB delete MSSQLLocalDB command.

Where are LocalDB files stored?

The system database files for the database are stored in the local AppData path, which is normally hidden. For example, C:\Users\<user>\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\LocalDBApp1\ .

How do I delete a database file?

To delete data or log files from a databaseExpand Databases, right-click the database from which to delete the file, and then click Properties. Select the Files page. In the Database files grid, select the file to delete and then click Remove. Click OK.

Does dropping a database delete the files?

Dropping a database deletes the database from an instance of SQL Server and deletes the physical disk files used by the database. If the database or any one of its files is offline when it is dropped, the disk files are not deleted. These files can be deleted manually by using Windows Explorer.


4 Answers

In this situation, detach the database rather than trying to drop it. In SQL Management Studio, right-click on the database, select "Tasks" then "Detach".

like image 173
IRM Avatar answered Oct 04 '22 03:10

IRM


All you need to do is to recreate the instance, tested with SQL 2012 ! just go in command prompt with admin rights and type:

//list the instancies sqllocaldb i  //stop selected instance sqllocaldb p "selected instance"  //delete sqllocaldb d "selected instance"  //recreate or create new one  sqllocaldb c "new instance" 
like image 28
Filip Gjorgjevikj Avatar answered Oct 04 '22 04:10

Filip Gjorgjevikj


I had the same problem. When designing DB using code first, I simply remove old DBs. It ends up with multiple deleted DB appearing in SQL Server Management Studio. Then when I try to query the DB, it becomes difficult to find the correct DB instance from amongst the deleted.

As IRM suggested, I tried to Detach those deleted DBs, and for some of them it works great!

However still I have several left. Then I tried "Take offline" on those DBs. Each time when I tried to take DB offline, the SQL Server Management Studio crashed. After SQL Server Management Studio restarted, the DB was gone. So try to do "take offline" if detach and delete don't work for you.

like image 40
Qixing Avatar answered Oct 04 '22 03:10

Qixing


I'd collected hundreds of these, and detaching them individually was just too damned tedious.

What I did:

SELECT 'EXEC sp_detach_db ''' + name + ''''
FROM sys.databases
;

This gave me a list of exec commands:

EXEC sp_detach_db 'E:\...\ADATABASE.MDF'
EXEC sp_detach_db 'E:\...\ANOTHERDATABASE.MDF'
EXEC sp_detach_db 'E:\...\ATHIRDDATABASE.MDF'
....
EXEC sp_detach_db 'master'
EXEC sp_detach_db 'model'
EXEC sp_detach_db 'msdb'
EXEC sp_detach_db 'tempdb'

Copy the results back into the command window, highlight everything other than the system databases, and execute.

like image 29
Jeff Dege Avatar answered Oct 04 '22 05:10

Jeff Dege