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

Cassandra deletes the key space

May 17, 2021 Cassandra

Table of contents

Use Cqlsh to remove the key space

You can use the command DROP KEYSPACE to remove KeySpace. Here's the syntax for deleting KeySpace.


DROP KEYSPACE <identifier>

That is:

DROP KEYSPACE “KeySpace name”


The following code removes keyspace tutorialspoint .

cqlsh> DROP KEYSPACE tutorialspoint;


Use the command Describe to verify the key space and check to see if the table is deleted, as shown below.

cqlsh> DESCRIBE keyspaces;

system system_traces

Since we've removed keyspace tutorialspoint, you won't find it in the keyspace list.

Use the Java API to delete the key space

You can use the Execute() method of the Session class to create a key space. Follow these steps to remove the key space using the Java API.

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

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

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 it to this method in string format will execute on cqlsh.

In the following example, we'll remove the keyspace named tp. You must store the query in a string variable and pass it to the execute() method, as shown below.

String query = "DROP KEYSPACE tp; ";


Here's a complete program for creating and using key spaces in Cassandra using the Java API.

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

public class Drop_KeySpace {

   public static void main(String args[]){

      String query = "Drop KEYSPACE tp";

      //creating Cluster object
      Cluster cluster = Cluster.builder().addContactPoint("").build();
      //Creating Session object
      Session session = cluster.connect();
      //Executing the query
      System.out.println("Keyspace deleted");

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 Delete_KeySpace

Under normal conditions, it should produce the following output:

Keyspace deleted