Delete rows (Java)
Finds rows in a table using filter clauses, and then deletes those rows.
|
If you don’t use a filter, or if you use an empty filter, then this method deletes all rows in the table. |
Deleting rows produces tombstones. Excessive tombstones can impact query performance.
For general information about working with tables and rows, see About tables with the Data API (Java).
|
Ready to write code? See the examples for this method to get started. If you are new to the Data API, check out the quickstart. |
Result
Deletes rows that match the specified parameters. If no rows match the specified parameters, this method does not delete any rows.
Does not return anything.
Parameters
Use the deleteMany method, which belongs to the com.datastax.astra.client.tables.Table class.
Method signature
void deleteMany(Filter filter)
void deleteMany(
Filter filter,
TableDeleteManyOptions options
)
| Name | Type | Summary | ||
|---|---|---|---|---|
|
The filter criteria, defined using the Data API filter syntax. If not empty, the filter depends on the table’s primary key definition:
|
|||
|
General API options for this operation, including the timeout. |
Examples
The following examples demonstrate how to delete rows in a table.
Delete a row by primary key
If the filter specifies the full primary key, only the row matching that full primary key is deleted.
In the following example, the table has a composite primary key with the columns title and author.
For more information, see Primary keys in tables (Java) and Filter operators for tables (Java).
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.definition.rows.Row;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
new DataAPIClient("APPLICATION_TOKEN")
.getDatabase("API_ENDPOINT")
.getTable("TABLE_NAME");
// Delete rows
Filter filter =
Filters.and(
Filters.eq("title", "Hidden Shadows of the Past"),
Filters.eq("author", "John Anthony"));
table.deleteMany(filter);
}
}
Delete rows in a table with a single-column primary key
If the table has a single-column primary key, the filter must include the column in the primary key, and it cannot include any other columns.
In the following example, the table has a single-column primary key with the column title.
For more information, see Primary keys in tables (Java) and Filter operators for tables (Java).
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.definition.rows.Row;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
new DataAPIClient("APPLICATION_TOKEN")
.getDatabase("API_ENDPOINT")
.getTable("TABLE_NAME");
// Delete rows
Filter filter = Filters.eq("title", "Hidden Shadows of the Past");
table.deleteMany(filter);
}
}
Delete rows in a table with a composite primary key
If the table has a composite primary key, the filter must include all columns in the primary key definition (all partition keys), and it cannot include any other columns.
In the following example, the table has a composite primary key with the columns title and author.
For more information, see Primary keys in tables (Java) and Filter operators for tables (Java).
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.definition.rows.Row;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
new DataAPIClient("APPLICATION_TOKEN")
.getDatabase("API_ENDPOINT")
.getTable("TABLE_NAME");
// Delete rows
Filter filter =
Filters.and(
Filters.eq("title", "Hidden Shadows of the Past"),
Filters.eq("author", "John Anthony"));
table.deleteMany(filter);
}
}
Delete rows in a table with a compound primary key
If the table has a compound primary key, the filter must include all of the partition (grouping) columns in the primary key.
The filter can also include clustering (sorting) columns from the primary key:
-
The order of the clustering columns in the filter must match the order in the primary key definition. You can omit clustering keys, but you cannot skip any clustering keys. For example, if you have three clustering keys,
a,b, andc, then you can includeaand omitbandc. However, if you includeb, then you must also includea. -
The last clustering column can use range operators (
$gt,$gte,$lt, and$lte) or the$eqoperator. All other clustering columns can only use the$eqoperator. For example, if your table has three clustering keys,a,b, andc, then you can only use range operators withc.
The filter cannot include any other columns.
In the following example, the table has a compound primary key with the partition column title and the clustering column rating.
For more information, see Primary keys in tables (Java) and Filter operators for tables (Java).
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.definition.rows.Row;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
new DataAPIClient("APPLICATION_TOKEN")
.getDatabase("API_ENDPOINT")
.getTable("TABLE_NAME");
// Delete rows
Filter filter =
Filters.and(Filters.eq("title", "Hidden Shadows of the Past"), Filters.eq("rating", 4));
table.deleteMany(filter);
}
}
Delete all rows
To delete all rows, use an empty filter.
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.definition.rows.Row;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
new DataAPIClient("APPLICATION_TOKEN")
.getDatabase("API_ENDPOINT")
.getTable("TABLE_NAME");
// Delete rows
table.deleteMany(new Filter());
}
}
Client reference
For more information, see the client reference.