TRUNCATE
Removes all data from the specified table immediately and irreversibly. By extension, this also removes all data from any materialized views based on the truncated table.
Node responsiveness
TRUNCATE requires responses from all nodes.
When you run TRUNCATE, the database sends a request to all nodes to delete the SSTables for the truncated table.
Deletion waits until all nodes respond.
If any nodes are down or unresponsive, the command fails entirely and outputs an error like Unable to complete request: one or more nodes were unavailable.
To ensure nodes are up and responsive before running TRUNCATE, do the following:
-
Use the CQL shell
CONSISTENCYcommand to set the consistency level toALL.Although the consistency level isn’t directly relevant to
TRUNCATE, it ensures that all nodes are responsive. You can test node responsiveness with aSELECTstatement. If any nodes are down, the query fails because it didn’t receive a response from all nodes. -
Use
nodetool statusor some other tool to make sure all nodes are up and healthy.
Synopsis
TRUNCATE [ TABLE ] [<keyspace_name>.]<table_name> ;
| Syntax conventions | Description |
|---|---|
UPPERCASE |
Literal keyword. |
Lowercase |
Not literal. |
|
Variable value. Replace with a user-defined value. |
|
Optional.
Square brackets ( |
|
Group.
Parentheses ( |
|
Or.
A vertical bar ( |
|
Repeatable.
An ellipsis ( |
|
Single quotation ( |
|
Map collection.
Braces ( |
|
Set, list, map, or tuple.
Angle brackets ( |
|
End CQL statement.
A semicolon ( |
|
Separate the command line options from the command arguments with two hyphens ( |
|
Search CQL only: Single quotation marks ( |
|
Search CQL only: Identify the entity and literal value to overwrite the XML element in the schema and solrConfig files. |
Options
- TRUNCATE TABLE
-
The command to drop all rows from a table.
The short form is
TRUNCATE. - keyspace_name
-
Optional. Name of the keyspace containing the table to truncate. If not specified, the current keyspace is used.
- table_name
-
Name of the table to truncate.
|
On Linux systems, object names, such as keyspace and table names, are case-sensitive. CQL converts object names to lowercase unless enclosed in double quotation marks. |
Examples
-
Run the
TRUNCATEcommand with the name of the table to truncate:TRUNCATE country_flag;If you have multiple tables with the same name in different keyspaces, specify the keyspace to avoid ambiguity:
TRUNCATE cycling.country_flag; -
If needed, wait for the
TRUNCATEoperation to complete on all nodes.Because
TRUNCATEwaits for a response from all nodes, it can take some time to complete in clusters with slow nodes, many nodes, or geographically distant datacenters. If the operation fails, see Node responsiveness. -
Use a
SELECTstatement to verify that the table is empty.SELECT * FROM cycling.country_flag;country | cyclist_name | flag ---------+--------------+------ (0 rows)