How do I delete all data in a Cassandra column family?

Cassandra

Cassandra Problem Overview


I'm looking for a way to delete all of the rows from a given column family in cassandra.

This is the equivalent of TRUNCATE TABLE in SQL.

Cassandra Solutions


Solution 1 - Cassandra

You can use the truncate thrift call, or the TRUNCATE <table> command in CQL.

http://www.datastax.com/docs/1.0/references/cql/TRUNCATE

Solution 2 - Cassandra

You can also do this via Cassandra CQL.

$ cqlsh
Connected to Test Cluster at localhost:9160.
[cqlsh 4.1.1 | Cassandra 2.0.6 | CQL spec 3.1.1 | Thrift protocol 19.39.0]
Use HELP for help.
cqlsh> TRUNCATE my_keyspace.my_column_family;

Solution 3 - Cassandra

Its very simple in Astyanax. Just a Single Line statement

/* keyspace variable is Keyspace Type */
keyspace.truncateColumnFamily(ColumnFamilyName); 

Solution 4 - Cassandra

If you are using Hector it is easy as well:

cluster.truncate("our keyspace name here", "your column family name here");

Solution 5 - Cassandra

If you are using cqlsh, then you can either do it in 2 ways

  1. use keyspace; and then truncate column_family;
  2. truncate keyspace.column_family;

If you want to use DataStax Java driver, you can look at - http://www.datastax.com/drivers/java/1.0/com/datastax/driver/core/querybuilder/QueryBuilder.html or http://www.datastax.com/drivers/java/2.0/com/datastax/driver/core/querybuilder/Truncate.html

depending on your version.

Solution 6 - Cassandra

if you are working on cluster setup, truncate can only be used when all the nodes of the cluster are UP.

By using truncate, we will miss the data(we are not sure with the importance of the data)

So the very safe way as well a trick to delete data is to use COPY command,

  1. backup data using copy cassandra cmd
    copy tablename to 'path'

  2. duplicate the file using linux cp cmd
    cp 'src path' 'dst path'

  3. edit duplicate file in dst path, delete all lines expect first line.
    save the file.

  4. use copy cassandra cmd to import
    copy tablename from 'dst path'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIke WalkerView Question on Stackoverflow
Solution 1 - Cassandrathe paulView Answer on Stackoverflow
Solution 2 - CassandracevarisView Answer on Stackoverflow
Solution 3 - CassandraabhiView Answer on Stackoverflow
Solution 4 - CassandraEugenView Answer on Stackoverflow
Solution 5 - CassandraSumodView Answer on Stackoverflow
Solution 6 - CassandraSwam GuruView Answer on Stackoverflow