How to delete 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 request:

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

+4
source share
2 answers

Most likely, your database was disconnected when you tried to DROP . In this case, SQL Server leaves the db files in the file system. This is intentional behavior.

According to DROP DATABASE (Transact-SQL)

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

+5
source

To drop a database is simply to disable it. You need to manually delete the mdf and ldf files ( see here for how to find them); then refer to this image:

After that you can recreate the database.

+2
source

All Articles