Scylla Documentation Logo Documentation
  • Server
    • Scylla Open Source
    • Scylla Enterprise
    • Scylla Alternator
  • Cloud
    • Scylla Cloud
    • Scylla Cloud Docs
  • Tools
    • Scylla Manager
    • Scylla Monitoring Stack
    • Scylla Operator
  • Drivers
    • CQL Drivers
    • DynamoDB Drivers
Download
Menu
Scylla Java Driver Manual Query builder DELETE

DELETE¶

To start a DELETE query, use one of the deleteFrom methods in QueryBuilder. There are several variants depending on whether your table name is qualified, and whether you use identifiers or raw strings:

import static com.datastax.oss.driver.api.querybuilder.QueryBuilder.*;

DeleteSelection delete = deleteFrom("user");

Note that, at this stage, the query can’t be built yet. You need at least one relation.

Selectors¶

A selector is something that appears after the DELETE keyword, and will be removed from the affected row(s).

Selectors are optional; if you don’t provide any, the whole row will be deleted.

The easiest way to add a selector is with a fluent API method:

deleteFrom("user").column("v1").column("v2");
// DELETE v1,v2 FROM user...

You can also create it manually with one of the factory methods in Selector, and then pass it to selector():

deleteFrom("user").selector(Selector.getColumn("v"))
// DELETE v FROM user ...

If you have multiple selectors, you can also use selectors() to add them all in a single call. This is a bit more efficient since it creates less temporary objects:

deleteFrom("user").selectors(getColumn("v1"), getColumn("v2"));
// DELETE v1,v2 FROM user...

Only 3 types of selectors can be used in DELETE statements:

  • simple columns (as illustrated in the previous examples);

  • fields in non-nested UDT columns:

    deleteFrom("user").field("address", "street");
    // DELETE address.street FROM user ...
    
  • elements in non-nested collection columns:

    deleteFrom("product").element("features", literal("color"));
    // DELETE features['color'] FROM product ...
    

You can also pass a raw CQL snippet, that will get appended to the query as-is, without any syntax checking or escaping:

deleteFrom("user").raw("v /*some random comment*/")
// DELETE v /*some random comment*/ FROM user ...

This should be used with caution, as it’s possible to generate invalid CQL that will fail at execution time; on the other hand, it can be used as a workaround to handle new CQL features that are not yet covered by the query builder.

Timestamp¶

The USING TIMESTAMP clause specifies the timestamp at which the mutation will be applied. You can pass either a literal value:

deleteFrom("user").column("v").usingTimestamp(1234)
// DELETE v FROM user USING TIMESTAMP 1234

Or a bind marker:

deleteFrom("user").column("v").usingTimestamp(bindMarker())
// DELETE v FROM user USING TIMESTAMP ?

If you call the method multiple times, the last value will be used.

Relations¶

Relations get added with the fluent whereXxx() methods:

deleteFrom("user").whereColumn("k").isEqualTo(bindMarker());
// DELETE FROM user WHERE k=?

Or you can build and add them manually:

deleteFrom("user").where(
    Relation.column("k").isEqualTo(bindMarker()));
// DELETE FROM user WHERE k=?

Once there is at least one relation, the statement can be built:

SimpleStatement statement = deleteFrom("user").whereColumn("k").isEqualTo(bindMarker()).build();

Relations are a common feature used by many types of statements, so they have a dedicated page in this manual.

Conditions¶

Conditions get added with the fluent ifXxx() methods:

deleteFrom("user")
    .whereColumn("k").isEqualTo(bindMarker())
    .ifColumn("v").isEqualTo(literal(1));
// DELETE FROM user WHERE k=? IF v=1

Or you can build and add them manually:

deleteFrom("user")
    .whereColumn("k").isEqualTo(bindMarker())
    .if_(
        Condition.Column("v").isEqualTo(literal(1)));
// DELETE FROM user WHERE k=? IF v=1

Conditions are a common feature used by UPDATE and DELETE, so they have a dedicated page in this manual.

PREVIOUS
Conditions
NEXT
Idempotence in the query builder
  • 4.13.0.x
    • 4.13.0.x
    • 4.12.0.x
    • 4.11.1.x
    • 4.10.0.x
    • 4.7.2.x
    • 3.11.2.x
    • 3.11.0.x
    • 3.10.2.x
    • 3.7.2.x
  • Java Driver for Scylla and Apache Cassandra®
  • API Documentation
  • Manual
    • API conventions
    • Case sensitivity
    • Core driver
      • Address resolution
      • Asynchronous programming
      • Authentication
      • Bill of Materials (BOM)
      • Compression
      • Configuration
        • Reference configuration
      • Control connection
      • Custom codecs
      • Detachable types
      • Using the driver in GraalVM native images
      • Query idempotence
      • Integration
      • Load balancing
      • Logging
      • Metadata
        • Node metadata
        • Schema metadata
        • Token metadata
      • Metrics
      • Native protocol
      • Non-blocking programming
      • Paging
      • Performance
      • Connection pooling
      • Query timestamps
      • Reactive Style Programming
      • Reconnection
      • Request tracker
      • Retries
      • Using the shaded JAR
      • Speculative query execution
      • SSL
      • Statements
        • Batch statements
        • Per-query keyspace
        • Prepared statements
        • Simple statements
      • Temporal types
      • Request throttling
      • Query tracing
      • Tuples
      • User-defined types
    • Developer docs
      • Administrative tasks
      • Common infrastructure
        • Concurrency
        • Driver context
        • Event bus
      • Native protocol layer
      • Netty pipeline
      • Request execution
    • Mapper
      • Integration
        • Kotlin
        • Lombok
        • Java 14 Records
        • Scala
      • DAOs
        • Custom result types
        • Delete methods
        • GetEntity methods
        • Increment methods
        • Insert methods
        • Null saving strategy
        • Query methods
        • Query provider methods
        • Select methods
        • SetEntity methods
        • Statement attributes
        • Update methods
      • Entities
      • Mapper interface
    • OSGi
    • Query builder
      • Conditions
      • DELETE
      • Idempotence in the query builder
      • INSERT
      • Relations
      • Schema builder
        • Aggregate
        • Function
        • Index
        • Keyspace
        • Materialized View
        • Table
        • Type
      • SELECT
      • Terms
      • TRUNCATE
      • UPDATE
  • Upgrade guide
  • Frequently asked questions
  • Changelog
  • Create an issue
  • Edit this page

On this page

  • DELETE
    • Selectors
    • Timestamp
    • Relations
    • Conditions
Logo
Docs Contact Us About Us
Mail List Icon Slack Icon
© 2022, ScyllaDB. All rights reserved.
Last updated on 25 May 2022.
Powered by Sphinx 4.3.2 & ScyllaDB Theme 1.2.2