Tuesday, March 3, 2015

What happens when we drop a database that is offline?

If you are thinking that dropping an offline database will give you an error then you are wrong. If the database or any one of its files is offline when it is dropped, the disk files are not deleted. Those files have to be deleted manually from the respective windows folder. 

Lets create a sample database and then we'll see the same.

Use Master
Go
IF NOT EXISTS ( SELECT name FROM sys.databases WHERE name = 'TestOffline' )
Create Database TestOffline
Go


Now put the database in Offline Mode by executing below T-Sql Statement:

Alter Database TestOffline Set Offline
Go

We can Verify from below screen shot that this database is now offline


Now Lets drop this database:

Drop Database TestOffline
Go

"Command(s) completed successfully." Now lets open the location where mdf and ldf are stored on disk. We can see that files are still present there.


So, It's clear that if we drop a database that is offline then the files(.mdf and .ldf) will not be deleted from disk. 

Thanks!!!



No comments:

Post a Comment