Delete rows
This Astra DB Serverless feature is currently in public preview. Development is ongoing, and the features and functionality are subject to change. Astra DB Serverless, and the use of such, is subject to the DataStax Preview Terms. The Data API tables commands are available through HTTP and the clients. If you use a client, tables commands are available only in client versions 2.0-preview or later. For more information, see Data API client upgrade guide. |
Finds rows in a table using filter clauses, and then deletes those rows.
For general information about working with tables and rows, see About tables.
Method signature
-
Python
-
TypeScript
-
Java
-
curl
The following method belongs to the astrapy.Table
class.
delete_many(
filter: Dict[str, Any],
*,
general_method_timeout_ms: int,
request_timeout_ms: int,
timeout_ms: int,
) -> None
The following method belongs to the Table
class.
async deleteMany(
filter: TableFilter<WSchema>,
timeout?: number | TimeoutDescriptor,
): void
The following methods belong to the com.datastax.astra.client.tables.Table
class.
void deleteMany(Filter filter)
void deleteMany(
Filter filter,
TableDeleteManyOptions options
)
curl -sS -L -X POST "ASTRA_DB_API_ENDPOINT/api/json/v1/ASTRA_DB_KEYSPACE/ASTRA_DB_TABLE" \
--header "Token: ASTRA_DB_APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"deleteMany": {
"filter": FILTER,
}
}'
Result
-
Python
-
TypeScript
-
Java
-
curl
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.
Deletes rows that match the specified parameters. If no rows match the specified parameters, this method does not delete any rows.
Returns a promise that resolves once the operation completes.
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.
Deletes rows that match the specified parameters. If no rows match the specified parameters, this method does not delete any rows.
Always returns a status.deletedCount
of -1
, regardless of whether a row was found and deleted.
Example response:
{
"status": {
"deletedCount": -1
}
}
Parameters
-
Python
-
TypeScript
-
Java
-
curl
Name | Type | Summary | ||
---|---|---|---|---|
|
|
A filter dictionary to specify the rows to delete.
delete_many filter examplesFor example, if a table is partitioned by columns
Invalid filter examples:
For information about operators, see Filter operators for tables. |
||
|
|
A timeout, in milliseconds, to impose on the underlying API request. If not provided, the Table defaults apply. This parameter is aliased as |
Name | Type | Summary | ||
---|---|---|---|---|
|
|
An object containing the filter to apply to the table to determine which rows to delete. It can be empty or contain key-value pairs that determine the rows to delete.
If not empty, the contents of
For information about operators, see Filter operators for tables. |
||
|
|
The client-side timeout for this operation. |
Name | Type | Summary |
---|---|---|
|
A filter expressing which condition the returned row must satisfy, which can be empty or use operators to compare columns with literal values.
Filters can be instantiated with its constructor and specialized with method If not empty, the contents of * For single-column primary keys, For information about operators, see Filter operators for tables. |
|
|
Operations to be applied to the delete operation like (mostly) timeout. |
Name | Type | Summary | ||
---|---|---|---|---|
|
|
The Data API command to delete any rows in a table that match a given |
||
|
|
Can be empty or contain key-value pairs that determine the rows to delete.
If not empty, the contents of
For information about operators, see Filter operators for tables. |
Examples
The following examples demonstrate how to delete rows in a table.
-
Python
-
TypeScript
-
Java
-
curl
Delete one single row (by specifying a full primary key):
my_table.delete_many({"match_id": "fight4", "round": 1})
Delete part of a partition (by specifying a range within a partition):
my_table.delete_many({"match_id": "fight5", "round": {"$gte": 5}})
Delete a whole partition (by specifying the "partitionBy" column values):
my_table.delete_many({"match_id": "fight7"})
An empty filter deletes all rows and completely empties the table:
|
Example:
Full script
from astrapy import DataAPIClient
client = DataAPIClient("TOKEN")
database = client.get_database("API_ENDPOINT")
from astrapy.constants import SortMode
from astrapy.info import (
CreateTableDefinition,
ColumnType,
)
my_table = database.create_table(
"games",
definition=(
CreateTableDefinition.builder()
.add_column("match_id", ColumnType.TEXT)
.add_column("round", ColumnType.TINYINT)
.add_vector_column("m_vector", dimension=3)
.add_column("score", ColumnType.INT)
.add_column("when", ColumnType.TIMESTAMP)
.add_column("winner", ColumnType.TEXT)
.add_set_column("fighters", ColumnType.UUID)
.add_partition_by(["match_id"])
.add_partition_sort({"round": SortMode.ASCENDING})
.build()
),
)
from astrapy.data_types import (
DataAPISet,
DataAPITimestamp,
DataAPIVector,
)
from astrapy.ids import UUID
insert_result = my_table.insert_many(
[
{
"match_id": "fight4",
"round": 1,
"winner": "Victor",
"score": 18,
"when": DataAPITimestamp.from_string(
"2024-11-28T11:30:00Z",
),
"fighters": DataAPISet([
UUID("0193539a-2770-8c09-a32a-111111111111"),
UUID('019353e3-00b4-83f9-a127-222222222222'),
]),
"m_vector": DataAPIVector([0.4, -0.6, 0.2]),
},
{"match_id": "fight5", "round": 1, "winner": "Adam"},
{"match_id": "fight5", "round": 2, "winner": "Betta"},
{"match_id": "fight5", "round": 3, "winner": "Caio"},
{"match_id": "fight7", "round": 1, "winner": "Joy"},
{"match_id": "fight7", "round": 2, "winner": "Kevin"},
{"match_id": "fight7", "round": 3, "winner": "Lauretta"},
],
)
# Delete a single row (full primary key specified):
my_table.delete_many({"match_id": "fight4", "round": 1})
# Delete part of a partition (inequality on the
# last-mentioned 'partitionSort' column):
my_table.delete_many({"match_id": "fight5", "round": {"$gte": 5}})
# Delete a whole partition (leave 'partitionSort' unspecified):
my_table.delete_many({"match_id": "fight7"})
# empty the table entirely with empty filter (CAUTION):
my_table.delete_many({})
# Delete a single row (full primary key specified):
my_table.delete_many({"match_id": "fight4", "round": 1})
# Delete part of a partition (inequality on the
# last-mentioned 'partitionSort' column):
my_table.delete_many({"match_id": "fight5", "round": {"$gte": 5}})
# Delete a whole partition (leave 'partitionSort' unspecified):
my_table.delete_many({"match_id": "fight7"})
# empty the table entirely with empty filter (CAUTION):
my_table.delete_many({})
Delete one single row (by specifying a full primary key):
await table.deleteMany({ matchId: 'fight4', round: 1 });
Delete part of a partition (by specifying a range within a partition):
await table.deleteMany({ matchId: 'fight5', round: { $gte: 5 } });
Delete a whole partition (by specifying the "partitionBy" column values):
await table.deleteMany({ matchId: 'fight7' });
An empty filter deletes all rows and completely empties the table:
|
Example:
Full script
import { CreateTableDefinition, DataAPIClient, InferTablePrimaryKey, InferTableSchema, timestamp, uuid, vector } from '@datastax/astra-db-ts';
// Instantiate the client and connect to the database
const client = new DataAPIClient();
const db = client.db(process.env.CLIENT_DB_URL!, { token: process.env.CLIENT_DB_TOKEN! });
// Create table schema using bespoke Data API table definition syntax, and then infer the type.
// For information about table typing and definitions, see the documentation for createTable.
const TableDefinition = <const>{
columns: {
matchId: 'text'
round: 'tinyint',
mVector: { type: 'vector', dimension: 3 },
score: 'int',
when: 'timestamp',
winner: 'text',
fighters: { type: 'set', valueType: 'uuid' },
},
primaryKey: {
partitionBy: ['matchId'],
partitionSort: { round: 1 },
},
} satisfies CreateTableDefinition;
type TableSchema = InferTableSchema<typeof TableDefinition>;
(async function () {
// Create a table with the given TableSchema type if a 'games' table doesn't already exist
const table = await db.createTable<TableSchema>('games', { definition: TableDefinition, ifNotExists: true });
// Insert some rows in an unordered fashion.
await table.insertMany([
{ matchId: 'fight4', round: 1, winner: 'Victor' },
{ matchId: 'fight5', round: 1, winner: 'Adam' },
{ matchId: 'fight5', round: 2, winner: 'Betta' },
{ matchId: 'fight5', round: 3, winner: 'Caio' },
{ matchId: 'fight7', round: 1, winner: 'Joy' },
{ matchId: 'fight7', round: 2, winner: 'Kevin' },
{ matchId: 'fight7', round: 3, winner: 'Lauretta' },
]);
// Use deleteOne and deleteMany to remove rows from a table.
// deleteOne examples (with 'find' to demonstrate that the row was deleted)
// Find rows where 'matchId' is 'fight7'
await table.find({ matchId: 'fight7' }).toArray().then(rs => console.log(rs.length));
// Delete one of the 'fight7' rows
await table.deleteOne({ matchId: 'fight7', round: 2 });
// Find the 'fight7' rows again
// The deleted row should not be returned
await table.find({ matchId: 'fight7' }).toArray().then(rs => console.log(rs.length));
// Attempt to delete the same 'fight7' row again
// Although the operation succeeds, nothing happens if the row was already deleted
await table.deleteOne({ matchId: 'fight7', round: 2 });
// Find the 'fight7' rows once again
// The same rows are returned as the previous attempt because the row was already deleted
await table.find({ matchId: 'fight7' }).toArray().then(rs => console.log(rs.length));
// deleteMany examples
// To use deleteMany to delete one row, specify the full primary key
await table.deleteMany({ matchId: 'fight4', round: 1 });
// To delete part of a partition, use an inequality operator on the table's final 'partitionSort' column
// This example deletes all rows where 'matchId' is 'fight5' and 'round' is greater than or equal to 5
await table.deleteMany({ matchId: 'fight5', round: { $gte: 5 } });
// To delete an entire partition, do not specify 'partitionSort' columns
// This example deletes all rows where 'matchId' is 'fight6'
await table.deleteMany({ matchId: 'fight7' });
// To delete all rows in the table, pass an empty filter
await table.deleteMany({});
// Uncomment the following line to drop the table and any related indexes.
// await table.drop();
})();
// To use deleteMany to delete one row, specify the full primary key
await table.deleteMany({ matchId: 'fight4', round: 1 });
// To delete part of a partition, use an inequality operator on the table's final 'partitionSort' column
// This example deletes all rows where 'matchId' is 'fight5' and 'round' is greater than or equal to 5
await table.deleteMany({ matchId: 'fight5', round: { $gte: 5 } });
// To delete an entire partition, do not specify 'partitionSort' columns
// This example deletes all rows where 'matchId' is 'fight6'
await table.deleteMany({ matchId: 'fight7' });
// To delete all rows in the table, pass an empty filter
await table.deleteMany({});
Delete one single row (by specifying a full primary key):
Filter filter =
and(eq("match_id", "fight7"), eq("round", 2));
tableRow.deleteMany(filter);
Delete part of a partition (by specifying a range within a partition):
Filter filter2 =
and(eq("match_id", "fight5"), gte("round", 5));
tableRow.deleteMany(filter2);
Delete a whole partition (by specifying the "partitionBy" column values):
tableRow
.deleteMany(eq("match_id", "fight5"));
An empty filter deletes all rows and completely empties the table. Additionally, you can use the syntactic sugar method,
|
Example:
package com.datastax.astra.client.tables;
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.databases.Database;
import com.datastax.astra.client.tables.commands.options.TableDeleteManyOptions;
import com.datastax.astra.client.tables.commands.options.TableDeleteOneOptions;
import com.datastax.astra.client.tables.definition.rows.Row;
import static com.datastax.astra.client.core.query.Filters.and;
import static com.datastax.astra.client.core.query.Filters.eq;
import static com.datastax.astra.client.core.query.Filters.gte;
public class DeleteMany {
public static void main(String[] args) {
Database db = new DataAPIClient("token").getDatabase("endpoint");
Table<Row> tableRow = db.getTable("games");
// Update
Filter filter = and(
eq("match_id", "fight7"),
eq("round", 2));
tableRow.deleteMany(filter);
tableRow.deleteMany(filter, new TableDeleteManyOptions()
.timeout(1000));
Filter filter2 = and(
eq("match_id", "fight5"),
gte("round", 5));
tableRow.deleteMany(filter2);
}
}
Find rows matching a given filter, and then delete them:
curl -sS -L -X POST "ASTRA_DB_API_ENDPOINT/api/json/v1/ASTRA_DB_KEYSPACE/ASTRA_DB_TABLE" \
--header "Token: ASTRA_DB_APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"deleteMany": {
"filter": {
"FILTER_COLUMN": "FILTER_VALUE",
"FILTER_COLUMN": "FILTER_VALUE"
}
}
}' | jq
An empty
|
Client reference
-
Python
-
TypeScript
-
Java
-
curl
For more information, see the client reference.
For more information, see the client reference.
For more information, see the client reference.
Client reference documentation is not applicable for HTTP.