Deleting and truncating data

The DELETE statement removes data from one or more columns, or removes an entire row. The TRUNCATE statement removes all data from a table.

Use the DELETE statement to remove data from one or more selected columns (column data is replaced with a null value), or to remove the entire row when no column is specified.

The data is not removed from disk immediately. The data is marked with a tombstone and removed during a nodetool repair operation.

Use the TRUNCATE statement to remove all data from the specified table immediately.

The examples in this section use the rank_by_year_and_name table:

CREATE TABLE cycling.rank_by_year_and_name (
  race_year int,
  race_name text,
  cyclist_name text,
  rank int,
  PRIMARY KEY ((race_year, race_name), rank)
);

  1. This DELETE statement deletes the cyclist name:
    DELETE cyclist_name
    FROM cycling.rank_by_year_and_name
    WHERE race_year = 2015
      AND race_name = 'Tour of Japan - Stage 4 - Minami > Shinshu'
      AND rank = 3;
  2. To delete a row, omit the column names after the DELETE statement. This example deletes one row:
    DELETE
    FROM cycling.rank_by_year_and_name
    WHERE race_year = 2015
      AND race_name = 'Tour of Japan - Stage 4 - Minami > Shinshu'
      AND rank = 3;
  3. To delete multiple rows, use a WHERE clause that specifies multiple rows:
    DELETE
    FROM cycling.rank_by_year_and_name
    WHERE race_year = 2015
      AND race_name = 'Tour of Japan - Stage 4 - Minami > Shinshu';
  4. To perform the delete only if the specified row exists, use IF EXISTS:
    DELETE
    FROM cycling.rank_by_year_and_name
    WHERE race_year = 2014
      AND race_name = '4th Tour of Beijing'
      AND rank = 3
    IF EXISTS;
  5. To remove all data from the specified table, use TRUNCATE:
    TRUNCATE cycling.rank_by_year_and_name;