Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL LocalDB - Cannot delete a DB when its files are deleted

Tags:

ssms

localdb

How can I delete a SQL LocalDB database that has had its files delete?

Dropping the database yields this message:

Unable to open the physical file "C:\Users\Public\Documents\LocalDB.Tests.3d0d7339-7cf2-45fe-a83b-b5079112ab80.mdf". Operating system error 2: "2(The system cannot find the file specified.)".

File activation failure. The physical file name "C:\Users\Public\Documents\LocalDB.Tests.3d0d7339-7cf2-45fe-a83b-b5079112ab80_log.ldf" may be incorrect.

Running master.sp_databases actually doesn't show them, but the Management Studio does.

like image 384
Luke Puplett Avatar asked Dec 18 '22 20:12

Luke Puplett


1 Answers

The problem is that you are trying to drop a database when the physical file has been deleted or couldn't be found.

To get around this you can detach the database. Detaching will drop the database without attempting to remove the file from the filesystem.

EXEC sp_detach_db 'My_Db'
like image 90
Clint Avatar answered Dec 28 '22 09:12

Clint