简体   繁体   中英

How to drop all Tables and UDTs from a Cassandra Keyspace?

Is there any way to clear a keyspace in Cassandra? We cannot drop the keyspace directly, as we don't have rights to create or drop keyspaces. Thank you in advance!

PS we are using Cassandra 3.7.

You can do something like cqlsh -e 'use ;describe tables'. This will return a list of tables that you can use to create queries to delete those tables. Similar with udt's.

Implementing it via cqlsh is quite straightforward, and it's better than try to implement something using the custom code - there are nuances of the programmatic schema changes, that it's better to avoid:

Full script to drop everything is here:

cqlsh -e 'describe keyspace test;' > ks.cql
rm -f drop-all.cql
grep -e '^CREATE MATERIALIZED VIEW' ks.cql|sed -e 's|^CREATE MATERIALIZED VIEW \(.*\) ($|DROP MATERIALIZED VIEW \1;|' >> drop-all.cql
grep -e '^CREATE INDEX' ks.cql|sed -e 's|^CREATE INDEX \(.*\) ($|DROP INDEX \1;|' >> drop-all.cql
grep -e '^CREATE TABLE' ks.cql|sed -e 's|^CREATE TABLE \(.*\) ($|DROP TABLE \1;|' >> drop-all.cql
grep -e '^CREATE TYPE' ks.cql|sed -e 's|^CREATE TYPE \(.*\) ($|DROP TYPE \1;|' >> drop-all.cql
cqlsh -f drop-all.cql

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM