Posted on

If you’re working locally, building a new app from scratch, you might be in a position where you want to wipe the database and start clean – but SQL Server may block you if you’ve got any open connections or any other issues blocking you.

This should set you as the only user of the database, which will clear the way for dropping it without ever getting an error:

use master;
if exists (select 1 from sys.databases where name = 'SomeDatabase')
alter database SomeDatabase set single_user with rollback immediate;
drop database SomeDatabase;

View code on GitHub

Leave a Reply

Your email address will not be published. Required fields are marked *