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

南笙酒味 提交于 2019-11-28 19:09:17

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".

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"
Qixing

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!