When working with Cassandra, you might encounter an instance where you must flush your Cassandra cluster and start from scratch.
NOTE: In the context of this tutorial, flushing refers to the method of removing all the data from a Cassandra cluster.
This guide provides you with a way of removing all the data from a Cassandra cluster, allowing you to start from scratch. Remember that the method discussed in this tutorial also removes the security features such as users, roles, authentication methods, etc.
CAUTION: The methods illustrated in this article removes all the data from your Cassandra cluster. Do not use this in the production database unless you know what you are doing.
We are not responsible for any data loss due to procedures and methods from this article.
That being said, let’s dive in!
Method 1: Removing Cassandra Data Location
The first and most applicable method of flushing your Cassandra cluster is removing all the files and directories stored in the Cassandra data directory.
Cassandra stores all the data in the /var/lib/cassandra/.
To flush Cassandra, remove the following files and directories:
- data/
- commitlog/
- hints/
- saved_caches/
For example, the following commands remove the specified files and directories:
$ sudo rm -r /var/lib/cassandra/commitlog/
$ sudo rm -r /var/lib/cassandra/hints/
$ sudo rm -r /var/lib/cassandra/saved_caches
NOTE: If you are using a custom data location, replace the /var/lib/cassandra with the path of your Cassandra’s cluster data directory.
Once you remove the data files from your cluster, restart your cluster starting from the seed node. Again, ensure not to modify the “cassandra.yml” file before reinitializing the nodes.
Method 2: Deleting Keyspaces Recursively
In some cases, you may want to preserve the users and roles in the cluster. Then, you can skip removing the data directories and run a simple recursive delete.
The command is as shown:
Edit the file using the following command:
Add the script using the following command:
for k in $keyspaces; do
echo Removing keyspace -> $k
echo "drop keyspace $k;" | cqlsh
done
Set the correct permissions.
Run the script using the following command:
The previous script starts by fetching all the keyspaces in the cluster using the DESC KEYSPACES command.
Next, we select the keyspaces that do not match the ^system glob. This filters the system keyspaces from the result, allowing you to preserve the features such as users, roles, etc.
In the next step, we loop over each keyspace in the cluster and pass the result to the DROP KEYSPACE command. This allows Cassandra to drop each key space for every iteration.
Conclusion
You learned two main methods of flushing your Cassandra cluster in this article. One mode allows you to preserve the features such as users while the other obliterates everything.
Thanks for reading!