Removes every table in a ClickHouse database by dropping the database itself or iterating through system.tables to execute DROP TABLE statements.
Run DROP DATABASE
to delete the database and all its tables, or query system.tables
to build individual DROP TABLE
statements. Both methods remove data permanently, so back up first.
Use DROP DATABASE
when you no longer need the schema. Its a single, atomic command, supports IF EXISTS
, ON CLUSTER
, and respects permissions. Recreate the database afterward if required.
DROP DATABASE [IF EXISTS] db_name
[ON CLUSTER cluster_name]
[NO DELAY]
Select table names from system.tables
, concatenate them into executable SQL, then run those statements. This lets you keep grants, settings, and the database object itself.
SELECT concat(
'DROP TABLE IF EXISTS ',
name,
' ON CLUSTER default'
) AS stmt
FROM system.tables
WHERE database = 'ecommerce';
SET allow_experimental_object_type = 1; -- enable multi-statement execution
SELECT stmt FROM (
SELECT concat('DROP TABLE IF EXISTS ', name) AS stmt
FROM system.tables WHERE database = 'ecommerce'
) FORMAT Vertical; -- copy & execute
Back up data, disable replication temporarily to avoid lag, drop materialized views first, and wrap DDL in a maintenance window. Always include IF EXISTS
to avoid errors.
Skipping backups: Dropping tables is irreversible. Take a snapshot or use S3 backup before running the command.
Ignoring distributed tables: Dropping only local shards leaves distributed tables broken. Always drop both local and distributed versions or drop the database cluster-wide.
Yesa00include ON CLUSTER cluster_name
. ClickHouse propagates the DROP to every shard and replica, ensuring consistent schema removal.
Data parts are deleted asynchronously; disk space returns after background cleanup finishes. Monitor system.mutations
to see progress.
1a0Stop ingestion.
2a0Back up S3 storage.
3a0Run DROP DATABASE IF EXISTS ecommerce ON CLUSTER company_cluster;
4a0Verify via SHOW DATABASES
. Recreate schema if needed.
ClickHouse lacks TRUNCATE TABLE, but you can run ALTER TABLE ... DELETE WHERE 1
to remove all rows while keeping the schema.
No full table lock occurs; ClickHouse marks parts for deletion and removes them asynchronously, so normal queries keep running.
Query SELECT count() FROM system.tables WHERE database = 'ecommerce';
. A result of 0 means every table was successfully removed.