Reputation: 65547
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.
Upvotes: 54
Views: 65013
Reputation: 9161
You can use the truncate
thrift call, or the TRUNCATE <table>
command in CQL.
http://www.datastax.com/docs/1.0/references/cql/TRUNCATE
Upvotes: 84
Reputation: 473
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'
Upvotes: 0
Reputation: 3846
If you are using cqlsh, then you can either do it in 2 ways
use keyspace
; and then truncate column_family;
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.
Upvotes: 3
Reputation: 5794
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;
Upvotes: 9
Reputation: 2370
If you are using Hector it is easy as well:
cluster.truncate("our keyspace name here", "your column family name here");
Upvotes: 3
Reputation: 4792
Its very simple in Astyanax. Just a Single Line statement
/* keyspace variable is Keyspace Type */
keyspace.truncateColumnFamily(ColumnFamilyName);
Upvotes: 6