Skip to content
Advertisement

Bat File to Delete sql database

I am able to delete files/folders through the bat file fine, the problem comes when i need to delete old mdf and ldf files.

I get access denied error message.

Is there a way to overcome this in the bat file? without having to open sql managment studio 2008 and delete them there?

Things to note: At the start I do not specificly know what the database is called, just it’s location (c:sql)

Advertisement

Answer

You can use sqlcmd in a batch file to drop the database. Something like this:

sqlcmd -s dbserver -u username -p password -q "DROP DATABASE databasename"

Then you can delete the related mdf and ldf files.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement