Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drop all tables in a database in CockroachDB

Tags:

cockroachdb

Is there a simple command that would let me drop all of the tables in a database? I have users/grants set, so I don’t want to drop the database itself, just the tables within it.

like image 617
Alex Robinson Avatar asked Oct 30 '22 09:10

Alex Robinson


1 Answers

CockroachDB doesn't natively support dropping all tables without dropping the database containing them, but you can by running:

cockroach sql --format=csv -e 'SHOW TABLES FROM databasename' \
  | tail -n +3 \
  | xargs -n1 printf 'DROP TABLE databasename."%s";\n' \
  | cockroach sql

If you don't mind also dropping the database, you could just run DROP DATABASE databasename CASCADE

like image 75
Alex Robinson Avatar answered Jan 02 '23 19:01

Alex Robinson