Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
Log in  ::  Register  ::  Not logged in

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Drop all Databases (+the option to exclude one)

By Kecerdikan,

I made this Query because we fix databases localy on a workstation (we have no sql server) but when backup up the server they need to be deleted localy that could sometimes take a while.
U can run this Query from the QA or ad it as a Job it's possible to run it from an executable.

A little more about Keepdb and Previewdb.
If you want/need to keep more databases just add the line:
AND name NOT IN ('Keepdb1','Keepdb2','Keepdb3')
If you want/need to keep just one add the line:
AND name NOT IN ('Keepdb1')
Type the line right below this line:
SELECT name FROM sysdatabases WHERE sid 0x01

Previewdb: It means that you can see which databases will be dropped make sure that it looks like this
-- before: exec sp_executesql @SQL
print @SQL
If you happy with the result you can just remove the -- and print @SQL
Happy dropping

Total article views: 497 | Views in the last 30 days: 2
Related Articles




Database Sync



Database suspect



Database Synchronization

Database Synchronization


Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones