Apache Cassandra

Cassandra Drop Index

When working with Cassandra databases, you may encounter instances where you must delete one or more indexes on a table—whether performing cleanup operations or just modifications, learning to remove an index from a table can be beneficial.

This post will discuss how you can remove an existing index from a table using CQL commands.

Cassandra Create Sample Table

To illustrate how we can create and drop an index, let us start by creating a sample table as shown in the code below:

cqlsh:sample> create table sample.user_info( id uuid, full_name text, email text, phone int, country text, primary key(i
d));

The command above creates a table called user_info in the sample keyspace. Once we have the target table, we can create an index as shown in the command below:

cqlsh:sample> create index user_loc on sample.user_info (country);

The command above will create an index user_loc of the values of the country column. If you do not specify the index name, Cassandra will generate an index name with the format table_name_column_name_idx.

Cassandra Drop Index Command Syntax

The DROP INDEX command follows a syntax as provided in the snippet below:

DROP INDEX [IF EXISTS] [keyspace.]index_name

Note that you can use the IF EXISTS command to suppress errors if the target index does not exist on the keyspace.

You can skip the keyspace if the target keyspace is selected for use.

Example Use Case

The example below shows how to use the DROP INDEX command to delete the index user_loc in the sample keyspace.

Refer to the above commands.

cqlsh:sample> drop index if exists user_loc;

If you are not in the target keyspace, run the command:

cqlsh:sample> drop index if exists sample.user_loc;

Conclusion

This article illustrated how to create a simple index on a target table using the CREATE INDEX command. We also covered how to delete an existing index using the DELETE INDEX command.

Check out our Cassandra Create Index command tutorial to learn how to create various types of Cassandra indexes.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list