cockroachdb

Drop all tables in a database in 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.


Solution

  • 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