Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I Remove mdf and ldf File In SQL Server 2008?

I have this code:

IF EXISTS (SELECT * FROM sys.sysdatabases WHERE name='MyDatabase')
begin
   DROP database MyDatabase
end
Go

CREATE DATABASE MyDatabase

But I see this error after executing this query:

Cannot create file 'C:\Program Files\Microsoft SQL Server\MSSQL10.FARASQL\MSSQL\DATA\MyDatabase.mdf'

like image 815
ali khezri Avatar asked Dec 27 '22 10:12

ali khezri


1 Answers

Most likely your database was offline when you tried to DROP it. In such case SQL Server leaves db files on filesystem. It's intentional behavior.

According to DROP DATABASE (Transact-SQL)

Dropping a database deletes the database from an instance of SQL Server and deletes the physical disk files used by the database. If the database or any one of its files is offline when it is dropped, the disk files are not deleted. These files can be deleted manually by using Windows Explorer.

like image 189
peterm Avatar answered Jan 14 '23 00:01

peterm