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

Cassandra deletes the table

May 17, 2021 Cassandra

Table of contents

Use Cqlsh to delete the table

You can use the command Drop Table to delete the table. The syntax is as follows:


DROP TABLE <tablename>


The following code removes an existing table from KeySpace.

cqlsh:tutorialspoint> DROP TABLE emp;


Use the Describe command to verify that the table has been deleted. Because the emp table has been deleted, you will not find it in the column family list.

cqlsh:tutorialspoint> DESCRIBE COLUMNFAMILIES;


Use the Java API to delete the table

You can delete tables using the Execute() method of the Session class. Follow these steps to remove the table using the Java API.

Step 1: Create a cluster object

First, create an instance of the Cluster.builder class com.datastax.driver.core package, as follows:

//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 set the KeySpace name as an existing key space by passing it in string format to this method, as shown below.

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

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

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

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'll remove a table named emp. You must store the query in a string variable and pass it to the execute() method, as shown below.

// Query

String query = "DROP TABLE emp1;”;

The complete program for removing tables in Cassandra using the Java API is shown below.

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session;
public class Drop_Table {

   public static void main(String args[]){

      String query = "DROP TABLE emp1;";
      Cluster cluster = Cluster.builder().addContactPoint("").build();
      //Creating Session object
      Session session = cluster.connect("tp");
      //Executing the query
      System.out.println("Table dropped");

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 Drop_Table

Under normal conditions, it should produce the following output:

Table dropped