Therefore, you may encounter a scenario where you need to perform a reset on the database or a specific table. This post will teach you how to remove all the data from a Cassandra table without removing the table schema. This allows you to reset all the data and start from scratch.”
Cassandra Truncate Command
We can use the TRUNCATE command to remove all the data from a specific table while preserving the table layout and schema.
Once you run the TRUNCATE command, all the data is removed from the table, and it’s impossible to reverse unless you have a backup copy of the table. This operation is applied across all the nodes in your Cassandra cluster. This ensures that all the data is synchronized in all the nodes.
The command syntax is as shown:
If you are already in the target keyspace, you can use the syntax as shown:
Before running the TRUNCATE command, ensure the following:
- All the nodes in the cluster are up and running. This allows the truncate operation to be applied across the cluster.
- The target table exists in the keyspace.
Step 1 – Select the Target Table
The first step is to ensure the target table exists in the keyspace.
Create Keyspace
Before setting up any sample data, we need a database to store the data. We can do this by creating a sample keyspace.
The query is as provided below:
The query above creates a simple keyspace called store with a replication_factor of 3. If you are not familiar with Cassandra Keyspaces, check out our tutorial on the topic to learn more.
Once created, we can switch to the keyspace with the command:
Create Table
The next step is to create a table holding the structure for our data. Keep in mind that Cassandra can handle unstructured data. However, for simplicity, we will attempt to use a table structure.
We can run the query as shown:
The query above should create an inventory table with the specified columns and data types.
... VALUES (1, 'Meta Quest', 5, 399, true);
cqlsh:store> INSERT INTO inventory(product_id, product_name, quantity, price, availability) VALUES (2, 'Echo Dot', 10, 27, true);
cqlsh:store> INSERT INTO inventory(product_id, product_name, quantity, price, availability) VALUES (3, 'Tile Pro', 10, 26, false);
cqlsh:store> INSERT INTO inventory(product_id, product_name, quantity, price, availability) VALUES (4, 'Monitor', 100, 499, false);
cqlsh:store> INSERT INTO inventory(product_id, product_name, quantity, price, availability) VALUES (5, 'Laptop', 87, 700, true);
We can then query the table as:
SELECT * FROM inventory;
The query above should return the records stored in the table as shown:
Cassandra Delete All Rows
To remove all the data from the cluster while preserving the table schema, we can run the truncate command:
TRUNCATE TABLE store.inventory;
This command should clear all the data from the table while still preserving the table structure.
Conclusion
This article covered how to use the TRUNCATE TABLE command to remove the data from a given table without dropping the table or the table structure. This is a great command when resetting the table without recreating it.
Thanks for reading & Happy coding!!