Drop all Tables, Stored Procedures, Views and Triggers
Reset a development database quickly with ready-made cleanup scripts.
When rebuilding a development or test database from scratch, manually dropping every object one by one is tedious. SQL Server lets you generate and execute dynamic scripts that remove all tables, stored procedures, views, and triggers in one pass.
Drop All Triggers
EXEC sp_MSforeachtable @command1 = "DISABLE TRIGGER ALL ON '?'";
Drop All Foreign Keys (Required Before Dropping Tables)
EXEC sp_MSforeachtable @command1 = "ALTER TABLE ? NOCHECK CONSTRAINT ALL";
Drop All Views
DECLARE @sql NVARCHAR(MAX) = N''; SELECT @sql += 'DROP VIEW ' + QUOTENAME(name) + ';' FROM sys.views; EXEC sp_executesql @sql;
Drop All Stored Procedures
DECLARE @sql NVARCHAR(MAX) = N''; SELECT @sql += 'DROP PROCEDURE ' + QUOTENAME(name) + ';' FROM sys.procedures; EXEC sp_executesql @sql;
Drop All Tables
EXEC sp_MSforeachtable @command1 = "DROP TABLE ?";
Recommended Order
- Disable/drop triggers
- Drop or disable foreign key constraints
- Drop views
- Drop stored procedures
- Drop tables last
Following this order avoids dependency errors, since SQL Server won't let you drop a table that's still referenced by a constraint, view, or procedure.
A Safer SQL Server Script That Really Drops Everything
Disabling triggers or constraints with NOCHECK does not remove them. If the goal is an empty schema, generate real DROP statements from the system catalog. Foreign keys must go before tables, and the script below prints the commands first so you can review them.
DECLARE @sql NVARCHAR(MAX) = N'';
-- 1. foreign keys
SELECT @sql += N'ALTER TABLE ' + QUOTENAME(SCHEMA_NAME(t.schema_id)) + '.' + QUOTENAME(t.name)
+ N' DROP CONSTRAINT ' + QUOTENAME(fk.name) + ';' + CHAR(10)
FROM sys.foreign_keys fk JOIN sys.tables t ON fk.parent_object_id = t.object_id;
-- 2. views and procedures (table triggers are removed along with their tables)
SELECT @sql += N'DROP VIEW ' + QUOTENAME(SCHEMA_NAME(schema_id)) + '.' + QUOTENAME(name) + ';' + CHAR(10)
FROM sys.views WHERE is_ms_shipped = 0;
SELECT @sql += N'DROP PROCEDURE ' + QUOTENAME(SCHEMA_NAME(schema_id)) + '.' + QUOTENAME(name) + ';' + CHAR(10)
FROM sys.procedures WHERE is_ms_shipped = 0;
-- 3. tables last
SELECT @sql += N'DROP TABLE ' + QUOTENAME(SCHEMA_NAME(schema_id)) + '.' + QUOTENAME(name) + ';' + CHAR(10)
FROM sys.tables WHERE is_ms_shipped = 0;
PRINT @sql; -- review first
-- EXEC sp_executesql @sql; -- uncomment only on a dev/test database
Triggers attached to a table are removed automatically when that table is dropped. Database-level DDL triggers are separate and are listed in sys.triggers with parent_class = 0. Note that sp_MSforeachtable is undocumented, so a catalog-based script like this is easier to trust and maintain.
The MySQL Equivalent
In MySQL the simplest clean reset is dropping and recreating the whole database, which removes tables, views, routines and triggers in one statement:
DROP DATABASE IF EXISTS shop_dev;
CREATE DATABASE shop_dev CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;
If you lack permission to recreate the database, generate statements from information_schema and switch off foreign key checks for the session:
SET FOREIGN_KEY_CHECKS = 0;
SELECT CONCAT('DROP TABLE IF EXISTS `', table_name, '`;')
FROM information_schema.tables
WHERE table_schema = 'shop_dev' AND table_type = 'BASE TABLE';
SELECT CONCAT('DROP VIEW IF EXISTS `', table_name, '`;')
FROM information_schema.views WHERE table_schema = 'shop_dev';
SELECT CONCAT('DROP ', routine_type, ' IF EXISTS `', routine_name, '`;')
FROM information_schema.routines WHERE routine_schema = 'shop_dev';
-- run the generated statements, then:
SET FOREIGN_KEY_CHECKS = 1;
MySQL triggers disappear with their tables, so they need no separate step.
The SELECT CONCAT queries only produce text. Copy their output into the editor, check it, and then run it. Routines are listed with routine_type set to PROCEDURE or FUNCTION, so the generated line drops each with the correct keyword. Dropping a table does not remove the views built on it; they stay behind and fail when queried, which is why views get their own step.
Before You Press Execute
- Run
SELECT DB_NAME();in SQL Server orSELECT DATABASE();in MySQL to confirm where you are connected. - Take a backup, even of a test database, if anyone else uses it.
- Use an account that only has rights on development databases.
- Wrap the reset in a script that refuses to run unless the database name ends in something like
_devor_test; a simple guard like this prevents the most painful accident. - Remember that other schemas, users, functions, sequences and synonyms may still remain; extend the script if you need them gone.
Better Alternatives for Repeated Resets
If you reset a database often, keep the schema in migration files with a tool such as Flyway, Liquibase or your framework's migrations. Recreating from migrations gives a known, repeatable state and avoids keeping destructive scripts around. Containers are another option: start a fresh database container, load the schema, and throw it away when tests finish.