Delete rows reference
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. |
A row represents a single record of data in a table in an Astra DB Serverless database.
You use the Table
class to work with rows through the Data API clients.
For instructions to get a Table
object, see Work with tables.
For general information about working with rows, including common operations and operators, see Work with rows.
For more information about the Data API and clients, see Get started with the Data API.
Delete a row
Find and delete one row in a table.
-
Python
-
TypeScript
-
Java
-
curl
For more information, see the Client reference.
Find a row by its primary key, and then delete it:
my_table.delete_one({"match_id": "fight7", "round": 2})
Parameters:
Name | Type | Summary |
---|---|---|
|
|
Describes the full primary key of the row. The row matching that primary key will be deleted. Only the the |
|
|
A timeout, in milliseconds, to impose on the underlying API request. If not provided, the Table defaults apply. This parameter is aliased as |
Full example 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()
),
)
insert_result = my_table.insert_many(
[
{"match_id": "fight7", "round": 1, "winner": "Joy"},
{"match_id": "fight7", "round": 2, "winner": "Kevin"},
{"match_id": "fight7", "round": 3, "winner": "Lauretta"},
],
)
# Count the rows matching a certain filter
len(my_table.find({"match_id": "fight7"}).to_list())
# 3
# Delete a row belonging to the group
my_table.delete_one({"match_id": "fight7", "round": 2})
# Count again
len(my_table.find({"match_id": "fight7"}).to_list())
# 2
# Attempt the delete again (nothing to delete)
my_table.delete_one({"match_id": "fight7", "round": 2})
# The count is unchanged
len(my_table.find({"match_id": "fight7"}).to_list())
# 2
Example:
# Count the rows matching a certain filter
len(my_table.find({"match_id": "fight7"}).to_list())
# 3
# Delete a row belonging to the group
my_table.delete_one({"match_id": "fight7", "round": 2})
# Count again
len(my_table.find({"match_id": "fight7"}).to_list())
# 2
# Attempt the delete again (nothing to delete)
my_table.delete_one({"match_id": "fight7", "round": 2})
# The count is unchanged
len(my_table.find({"match_id": "fight7"}).to_list())
# 2
For more information, see the Client reference.
Find a row by its primary key, and then delete it:
await table.deleteOne({ matchId: 'fight7', round: 2 });
Parameters:
Name | Type | Summary |
---|---|---|
|
|
Describes the full primary key of the row. The row matching that primary key will be deleted. Only the the |
|
|
The client-side timeout for this operation. |
Returns:
Promise<void>
- A promise that resolves when the operation is complete.
Why void
?
The deleteMany
operation, as returned from the Data API, is always { deletedCount: -1 }
, regardless of how many rows were deleted. Therefore, void
is used.
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();
})();
// 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));
For more information, see the Client reference.
Find a row by its primary key, and then delete it:
Filter filter = and(
eq("match_id", "fight7"),
eq("round", 2));
// No options
tableRow.deleteOne(filter);
// Using Options
tableRow.deleteOne(filter, new TableDeleteOneOptions()
.timeout(1000));
Parameters:
Name | Type | Summary |
---|---|---|
|
Describes the row to delete by its primary key values. You cannot filter on non-primary keys. Only the the Filters can be instantiated with its constructor and specialized with method |
|
|
Operations to be applied to the delete operation like (mostly) timeout. |
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.TableUpdateOperation;
import com.datastax.astra.client.tables.commands.options.TableDeleteOneOptions;
import com.datastax.astra.client.tables.commands.options.TableUpdateOneOptions;
import com.datastax.astra.client.tables.definition.rows.Row;
import java.util.Set;
import static com.datastax.astra.client.core.query.Filters.and;
import static com.datastax.astra.client.core.query.Filters.eq;
public class DeleteOne {
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.deleteOne(filter);
tableRow.deleteOne(filter, new TableDeleteOneOptions()
.timeout(1000));
}
}
Find a row by its primary key, and then delete it:
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 '{
"deleteOne": {
"filter": {
"PRIMARY_KEY_COLUMN": "PRIMARY_KEY_VALUE"
}
}
}' | jq
Parameters:
Name | Type | Summary |
---|---|---|
|
|
The Data API command to find and delete the first row in a table that matches the given |
|
|
Key-value pairs describing the full primary key of the row to delete. Only the the |
Returns:
A well-formed request returns "deletedCount": -1
, regardless of whether a row was found or deleted.
A well-formed request against an empty table still returns "deletedCount": -1
.
Example response
{
"status": {
"deletedCount": -1
}
}
Delete rows
Delete multiple rows in a table.
-
Python
-
TypeScript
-
Java
-
curl
For more information, see the Client reference.
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:
|
Parameters:
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 Data API operators. |
||
|
|
A timeout, in milliseconds, to impose on the underlying API request. If not provided, the Table defaults apply. This parameter is aliased as |
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({})
Use this command to delete multiple rows from a table based on filter conditions.
Unlike collections, this operation is not paginated. All rows that match the filter are deleted in a single operation.
For more information, see the Client reference.
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:
|
Parameters:
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 Data API operators. |
||
|
|
The client-side timeout for this operation. |
Returns:
Promise<void>
- A promise that resolves when the operation is complete.
Why void
?
The deleteMany
operation, as returned from the Data API, is always { deletedCount: -1 }
, regardless of how many rows were deleted. Therefore, void
is used.
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({});
For more information, see the Client reference.
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,
|
Parameters:
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 Data API operators. |
|
|
Operations to be applied to the delete operation like (mostly) timeout. |
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
|
Parameters:
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 Data API operators. |
Returns:
A well-formed request returns "deletedCount": -1
, regardless of the number of rows actually deleted.
A well-formed request against an empty table still returns "deletedCount": -1
.
deleteMany
does not paginate the response.
If any rows matched the given filter, they are deleted in a single operation.
Example response
{
"status": {
"deletedCount": -1
}
}