Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Cassandra truncated the table

May 17, 2021 Cassandra

Table of contents

Truncation tables using Cqlsh

You can use the TRUNCATE command to truncate the table. W hen you truncated a table, all rows of the table are permanently deleted. The syntax of this command is given below.


TRUNCATE <tablename>


Let's assume that there is a table called student that has the following data.

s_id s_name s_branch s_aggregate
1 Ram IT 70
2 rahman EEE 75
3 robbin Mech 72

When you execute a select statement to get the table object, it gives you the following output.

cqlsh:tp> select * from student;

 s_id | s_aggregate | s_branch | s_name
    1 |          70 |       IT | ram
    2 |          75 |      EEE | rahman
    3 |          72 |     MECH | robbin

(3 rows)

Now use the TRUNCATE command to truncate the table.

cqlsh:tp> TRUNCATE student;


Verify that the table is truncated by executing the select statement. The output of the select statement on the student table after truncation is given below.

cqlsh:tp> select * from student;

 s_id | s_aggregate | s_branch | s_name

(0 rows)

Use the Java API to truncated the table

You can truncated the table using the execute() method of the Session class. Follow these steps to truncated the table.

Step 1: Create a cluster object

First, create an instance of the Cluster.builder class called com.datastax.driver.core, as shown below.

//Creating Cluster.Builder object
Cluster.Builder builder1 = Cluster.builder();

Use the addContactPoint() method of the Cluster.Builder object to add a contact point (the IP address of the node). This method returns Cluster.Builder.

//Adding contact point to the Cluster.Builder object
Cluster.Builder builder2 = build.addContactPoint( "" );

Create a cluster object with the new builder object. T o do this, there is a method called build() in the Cluster.Builder class. The following code shows how to create cluster objects.

//Building a cluster
Cluster cluster =;

You can use a single line of code to build cluster objects, as shown below.

Cluster cluster = Cluster.builder().addContactPoint("").build();

Step 2: Create a session object

Create an instance of a Session object using the Connect() method of the Cluster class, as shown below.

Session session = cluster.connect( );

This method creates a new session and initializes it. If you already have a key space, you can do so by formatting the KeySpace name as an existing key space in string format, as shown below.

Session session = cluster.connect(“ Your keyspace name ” );
Session session = cluster.connect(“ tp” );

Here we use a keyspace called tp. Therefore, the session object is created as follows.

Step 3: Execute the query

You can use the Execute() method of the Session class to perform CQL queries. P ass the query to the execute() method in string format or state class objects. Whether you pass this method in string format will execute on cqlsh.

In the following example, we truncated a table called emp. You must store the query in a string variable and pass it to the execute() method, as shown below.

String query = "TRUNCATE emp;;”;

The full program for truncated the tables in Cassandra using the Java API is shown below.

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session;

public class Truncate_Table {

   public static void main(String args[]){
      String query = "Truncate student;";
      //Creating Cluster object
      Cluster cluster = Cluster.builder().addContactPoint("").build();
      //Creating Session object
      Session session = cluster.connect("tp");
      //Executing the query
      System.out.println("Table truncated");

Use the class name and .java save the above program and browse to the save location. Compile and execute the program as shown in the following image.

$java Truncate_Table

Under normal conditions, it should produce the following output:

Table truncated