Sql-server – How to delete mdf file in use but not attached

sql server

I have a mdf file that is not attached but when I try to delete it Windows Explorer fails due to the file being in use. SQL Manager also says it's in use when I try to attach the MDF so that I can delete it. Is there a way to kill the connection and delete the file without stopping the SQL service? Thanks

Best Answer

Try to set the database (returned by the query in sys.master_files) Offline with

Alter database [YourDB] set offline;

If the database is still seen in the dmv, it may still be possible to set it as offline (and then you'll be able to delete the file).

But if you want to delete the file, you could also just try to drop the database

Drop database [YourDB];