SQL Error 02: SQL Server showing an "unable to open physical file" error ?
Why is SQL Server showing an "unable to open physical file" error after I’ve already deleted the database locally ?
It sounds like SQL Server still has some reference to the deleted database’s physical files. Here’s how to fix it:
-
Check if the database still exists in SQL Server:
SELECT name FROM sys.databases
If the database name is still there, it means SQL Server thinks it’s attached, even if the physical file is gone.
-
Detach the ghost database:
If the database exists but its files are deleted:USE master; ALTER DATABASE [YourDatabaseName] SET OFFLINE WITH ROLLBACK IMMEDIATE; DROP DATABASE [YourDatabaseName];
If that doesn’t work, try detaching:
EXEC sp_detach_db 'YourDatabaseName', 'true';
-
Manually clean up leftover files:
Even if the database is detached, some files (.mdf, .ldf) may still exist in the filesystem. Navigate to the directory where the files were stored and delete them if they’re still there. -
Restart SQL Server Service:
Sometimes SQL Server keeps a lock on files — restarting the service can help clear it. -
Reattach if needed:
If you didn’t actually mean to delete the database files and still have them somewhere:CREATE DATABASE [YourDatabaseName] ON (FILENAME = 'C:\Path\To\YourDatabase.mdf'), (FILENAME = 'C:\Path\To\YourDatabase.ldf') FOR ATTACH;
Comment about what the error says exactly if this doesn’t solve it!
0 Comments