How I completely remove a SQL Server database – Guaranteed to work – At least on my current setup

 

How I completely remove a SQL Server database – Guaranteed to work – At least on my current setup

  • Comments 1

I don't know about you, but I always seem to fumble around when I need to remove a database. Specifically, I want to remove the underlying physical database file. Most SQL Server Databases are just two files: an *.mdf file and an *.ldf (log file).

The typical “Drop” command doesn’t do everything I want it to

I can afford to be very decisive on my dev computer. These steps are dangerous because your data is permanently gone, so proceed at your own risk about what you delete. Don’t try this at home – it is for professionals only.

So I have 5 sure-fire steps that are brutal, but get the job done

Administrator Privileges
I run everything as administrator including (1) SQL Server Management Studio 2008 R2 and a (2) DOS command prompt.
Not running as administrator will give you a world of pain.

 

Step 1 - Figure out the folder of my physical database files (think .mdf and .ldf)

We will physically delete the database so we need to know where it lives. That is what this step is about.

This can be done by right mouse clicking on the database in SQL Server Management Studio (Process = SSMS) as follows:

SQL Server Management Studio
How to get the physical location of your database.
MyImage


Now find the physical database path. I recommend copying it to the clipboard. 

How to get the physical path
Click on “Files." You can see your file path below.

 

MyImage


Step 2 - Get an administrator command prompt. Run my batch file to stop the
relevant SQL Server 2008 R2 services:

   net stop "SQL Server (LOCALHOST)"  

   net stop "SQL Server (MSSQLSERVER)"  

   net stop "SQL Server (SQLEXPRESS)"

snap0321

Step 3 – Go to the physical location of the .mdf and .ldf files. Select "delete"

My directory is:
C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\

MyImage

snap0323

Step 4 - Restart SQL Server 2008 R2 services:

   net start "SQL Server (LOCALHOST)"  

   net start "SQL Server (MSSQLSERVER)"  

   net start "SQL Server (SQLEXPRESS)"

snap0324

Step 5 – Go back to SQL Server Management Studio 2008 R2

Right mouse click on the database
Select “delete”

MyImage

Confirm the delete
Click “OK”

MyImage

You are done 

You can start over and re-create the original database
The steps have completely eliminated the database. This has worked consistently for me for some time. Notice that MarketIndexData is gone for good.

If you’ve got improvements that work better, I’d love to hear it. bterkaly@microsoft.com
snap0327

  • ok, just gotta say.....PERFECT! Exactly!

Page 1 of 1 (1 items)
Leave a Comment
  • Please add 4 and 3 and type the answer here:
  • Post