Drop a SQL Server LocalDB Database When Its MDF File Is Missing

Deleting an MDF file outside SQL Server leaves the database registered even though it cannot be opened. If the database is disposable and cannot be restored, connect to master, force the broken database offline, and drop that database registration.

Last updated: October 23, 2026.

USE [master];
GO

ALTER DATABASE [AppDb]
SET OFFLINE WITH ROLLBACK IMMEDIATE;
GO

DROP DATABASE [AppDb];
GO

Replace AppDb only after confirming the exact name in sys.databases. This is destructive: use a backup or restore the missing files instead when the database contains data that must be preserved.

Run the command outside the broken database

Open the connection with Database=master; a connection whose initial catalog is the missing database can fail before the command runs. The Microsoft DROP DATABASE documentation states that a damaged database can normally be set offline and then dropped. It also warns that a database in use cannot be dropped.

WITH ROLLBACK IMMEDIATE disconnects other sessions and rolls back incomplete work. Keep ALTER DATABASE and DROP DATABASE together in the same administrative operation so another connection cannot interfere. Do not wrap DROP DATABASE in an explicit transaction.

Do not delete the whole LocalDB instance first

The SqlLocalDB utility can stop and delete an instance, but deleting the instance removes every database associated with it. Reserve that option for an intentionally disposable development instance after inventorying all attached databases.

After the drop, verify that the name is absent from sys.databases, then let the application create or attach a fresh database. If the old data or log file still exists, inspect and preserve it before recreation; Microsoft notes that files already offline are not deleted by DROP DATABASE. Prevent recurrence by having the application detach or drop disposable databases through SQL Server rather than deleting MDF and LDF files directly.

Continue with reading SQL Server setup logs, moving SQL Server database files safely, and restoring a database under another name.

Related Web Cheat Sheet guides

Sergey Kornilov

Sergey Kornilov