I use SQL Server 2005 Express user instances for unit testing. Each test randomly generates a database name, so the connection string looks like this:
Data Source=.\SQLEXPRESS2005;Database=MyTest_624332256;User Instance=true;Integrated Security=SSPI;
where 624332256 is a randomly generated number.
At the end I want to delete all those database files (.MDF and .LDF).
If I call sp_detach_db or even DROP DATABASE it deletes the DB instances from sys.databases, however the database files still remain in %LOCALAPPDATA%\Microsoft\Microsoft SQL Server Data\SQLEXPRESS2005
How do I delete the files, too? I thought DROP DATABASE was supposed to do this, but it doesn’t!
I ended up writing code to do this manually, querying for the physical file names: