Delete a document
Finds a single document in a collection using filter and sort clauses, and then deletes that document.
This method and the method to find and delete a document have the same database effect but differ in their return value. This method returns details about the success of the deletion. The method to find and delete a document returns the document that was found and deleted.
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
-
Python
-
TypeScript
-
Java
-
curl
Deletes a document that matches the specified parameters and returns a CollectionDeleteResult
object that includes details about the success of the operation.
Example response:
CollectionDeleteResult(deleted_count=1, raw_results=...)
Deletes a document that matches the specified parameters and returns a promise that resolves to a CollectionDeleteOneResult
object that includes details about the success of the operation.
Example resolved response:
{ deletedCount: 1 }
Deletes a document that matches the specified parameters and returns CollectionDeleteResult
, which includes the number of documents that were deleted.
Deletes a document that matches the specified parameters and returns an object that includes details about the number of documents that were deleted.
Example response:
{
"status": {
"deletedCount": 1
}
}
Parameters
-
Python
-
TypeScript
-
Java
-
curl
Use the delete_one
method, which belongs to the astrapy.Collection
class.
Method signature
delete_one(
filter: Dict[str, Any],
*,
sort: Dict[str, Any],
general_method_timeout_ms: int,
request_timeout_ms: int,
timeout_ms: int,
) -> CollectionDeleteResult
Name | Type | Summary |
---|---|---|
|
|
Optional. An object that defines filter criteria using the Data API filter syntax. The method only finds documents that match this filter criteria. For a list of available filter operators and more examples, see Filter operators for collections. Filters can use only indexed fields. If you apply selective indexing when you create a collection, you can’t reference non-indexed fields in a filter. |
|
|
Optional. Sorts documents by one or more fields, or performs a vector search. For more information, see Sort clauses for collections. Sort clauses can use only indexed fields. If you apply selective indexing when you create a collection, you can’t reference non-indexed fields in sort queries. For vector searches, this parameter can use either |
|
|
Optional. The maximum time, in milliseconds, that the client should wait for the underlying HTTP request. Default: The default value for the collection. This default is 30 seconds unless you specified a different default when you initialized the |
|
|
Optional.
An alias for |
|
|
Optional.
An alias for |
Use the deleteOne
method, which belongs to the Collection
class.
Method signature
async deleteOne(
filter: CollectionFilter<Schema>,
options?: {
sort?: Sort,
timeout?: number | TimeoutDescriptor,
},
): CollectionDeleteOneResult
Name | Type | Summary |
---|---|---|
|
An object that defines filter criteria using the Data API filter syntax. The method only finds documents that match this filter criteria. For a list of available filter operators and more examples, see Filter operators for collections. Filters can use only indexed fields. If you apply selective indexing when you create a collection, you can’t reference non-indexed fields in a filter. |
|
|
Optional.
The options for this operation. See the |
Name | Type | Summary |
---|---|---|
|
Optional. Sorts documents by one or more fields, or performs a vector search. For more information, see Sort clauses for collections. Sort clauses can use only indexed fields. If you apply selective indexing when you create a collection, you can’t reference non-indexed fields in sort queries. For vector searches, this parameter can use either |
|
|
|
Optional. include::api-reference:partial$ts-timeout-single-request.adoc |
Use the deleteOne
method, which belongs to the com.datastax.astra.client.Collection
class.
Method signature
CollectionDeleteResult deleteOne(
Filter filter
)
CollectionDeleteResult deleteOne(
Filter filter,
CollectionDeleteOneOptions options
)
Name | Type | Summary |
---|---|---|
|
|
An object that defines filter criteria using the Data API filter syntax. The method only finds documents that match this filter criteria. For a list of available filter operators and more examples, see Filter operators for collections. Filters can use only indexed fields. If you apply selective indexing when you create a collection, you can’t reference non-indexed fields in a filter. |
|
Optional. The options for this operation. See the methods of the `CollectionDeleteOneOptions ` class for more details. |
Method | Parameters | Summary |
---|---|---|
|
|
Optional. Sorts documents by one or more fields, or performs a vector search. For more information, see Sort clauses for collections. Sort clauses can use only indexed fields. If you apply selective indexing when you create a collection, you can’t reference non-indexed fields in sort queries. For vector searches, this parameter can use either |
Use the deleteOne
command.
Command signature
curl -sS -L -X POST "ASTRA_DB_API_ENDPOINT/api/json/v1/ASTRA_DB_KEYSPACE/COLLECTION_NAME" \
--header "Token: ASTRA_DB_APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"deleteOne": {
"filter": FILTER,
"sort": SORT
}
}'
Name | Type | Summary |
---|---|---|
|
|
An object that defines filter criteria using the Data API filter syntax. The method only finds documents that match this filter criteria. For a list of available filter operators and more examples, see Filter operators for collections. Filters can use only indexed fields. If you apply selective indexing when you create a collection, you can’t reference non-indexed fields in a filter. |
|
|
Optional. Sorts documents by one or more fields, or performs a vector search. For more information, see Sort clauses for collections. Sort clauses can use only indexed fields. If you apply selective indexing when you create a collection, you can’t reference non-indexed fields in sort queries. For vector searches, this parameter can use either |
Examples
The following examples demonstrate how to delete a document in a collection.
Delete a document by ID
All documents have a unique _id
property. You can use a filter to find a document with a specific _id
, and then delete that document.
-
Python
-
TypeScript
-
Java
-
curl
from astrapy import DataAPIClient
# Get an existing collection
client = DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
database = client.get_database("ASTRA_DB_API_ENDPOINT")
collection = database.get_collection("COLLECTION_NAME")
# Delete a document
result = collection.delete_one(
{"_id": "101"}
)
print(result)
import { DataAPIClient } from '@datastax/astra-db-ts';
// Get an existing collection
const client = new DataAPIClient('ASTRA_DB_APPLICATION_TOKEN');
const database = client.db('ASTRA_DB_API_ENDPOINT');
const collection = database.collection('COLLECTION_NAME');
// Delete a document
(async function () {
const result = await collection.deleteOne(
{ _id: "101" }
);
console.log(result);
})();
package com.examples;
import com.datastax.astra.client.collections.Collection;
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.collections.definition.documents.Document;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.collections.commands.results.CollectionDeleteResult;
public class DeleteOne {
public static void main(String[] args) {
// Get an existing collection
Collection<Document> collection = new DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
.getDatabase("ASTRA_DB_API_ENDPOINT")
.getCollection("COLLECTION_NAME");
// Delete a document
Filter filter = Filters.eq("_id", "101");
CollectionDeleteResult result = collection.deleteOne(filter);
System.out.println(result.getDeletedCount());
}
}
curl -sS -L -X POST "ASTRA_DB_API_ENDPOINT/api/json/v1/ASTRA_DB_KEYSPACE/ASTRA_DB_COLLECTION" \
--header "Token: ASTRA_DB_APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"deleteOne": {
"filter": {
"_id": "101"
}
}
}'
Delete a document that matches a filter
You can use a filter to find a document that matches specific criteria.
For example, you can find a document with an isCheckedOut
value of false
and a numberOfPages
value less than 300.
For a list of available filter operators and more examples, see Filter operators for collections.
Filters can use only indexed fields. If you apply selective indexing when you create a collection, you can’t reference non-indexed fields in a filter.
-
Python
-
TypeScript
-
Java
-
curl
from astrapy import DataAPIClient
# Get an existing collection
client = DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
database = client.get_database("ASTRA_DB_API_ENDPOINT")
collection = database.get_collection("COLLECTION_NAME")
# Delete a document
result = collection.delete_one(
{
"$and": [
{"isCheckedOut": False},
{"numberOfPages": {"$lt": 300}},
]
}
)
print(result)
import { DataAPIClient } from '@datastax/astra-db-ts';
// Get an existing collection
const client = new DataAPIClient('ASTRA_DB_APPLICATION_TOKEN');
const database = client.db('ASTRA_DB_API_ENDPOINT');
const collection = database.collection('COLLECTION_NAME');
// Delete a document
(async function () {
const result = await collection.deleteOne(
{
$and: [
{ isCheckedOut: false },
{ numberOfPages: { $lt: 300 } }
],
}
);
console.log(result);
})();
package com.examples;
import com.datastax.astra.client.collections.Collection;
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.collections.definition.documents.Document;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.collections.commands.results.CollectionDeleteResult;
public class DeleteOne {
public static void main(String[] args) {
// Get an existing collection
Collection<Document> collection = new DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
.getDatabase("ASTRA_DB_API_ENDPOINT")
.getCollection("COLLECTION_NAME");
// Delete a document
Filter filter = Filters.and(
Filters.eq("isCheckedOut", false),
Filters.lt("numberOfPages", 300));
CollectionDeleteResult result = collection.deleteOne(filter);
System.out.println(result.getDeletedCount());
}
}
curl -sS -L -X POST "ASTRA_DB_API_ENDPOINT/api/json/v1/ASTRA_DB_KEYSPACE/ASTRA_DB_COLLECTION" \
--header "Token: ASTRA_DB_APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"deleteOne": {
"filter": {"$and": [
{"isCheckedOut": false},
{"numberOfPages": {"$lt": 300}}
]}
}
}'
Delete a document that is most similar to a search vector
To find the document whose $vector
value is most similar to a given vector, use a sort with the vector embeddings that you want to match. For more information, see Find data with vector search.
Vector search is only available for vector-enabled collections. For more information, see $vector and $vectorize in collections.
-
Python
-
TypeScript
-
Java
-
curl
from astrapy import DataAPIClient
# Get an existing collection
client = DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
database = client.get_database("ASTRA_DB_API_ENDPOINT")
collection = database.get_collection("COLLECTION_NAME")
# Delete a document
result = collection.delete_one(
{},
sort={"$vector": [0.1, 0.2, 0.3]},
)
print(result)
import { DataAPIClient } from '@datastax/astra-db-ts';
// Get an existing collection
const client = new DataAPIClient('ASTRA_DB_APPLICATION_TOKEN');
const database = client.db('ASTRA_DB_API_ENDPOINT');
const collection = database.collection('COLLECTION_NAME');
// Delete a document
(async function () {
const result = await collection.deleteOne(
{},
{ sort: { $vector: [0.1, 0.2, 0.3] } },
);
console.log(result);
})();
package com.examples;
import com.datastax.astra.client.collections.Collection;
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.collections.definition.documents.Document;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.collections.commands.options.CollectionDeleteOneOptions;
import com.datastax.astra.client.collections.commands.results.CollectionDeleteResult;
import com.datastax.astra.client.core.query.Sort;
public class DeleteOne {
public static void main(String[] args) {
// Get an existing collection
Collection<Document> collection = new DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
.getDatabase("ASTRA_DB_API_ENDPOINT")
.getCollection("COLLECTION_NAME");
// Delete a document
Filter filter = new Filter();
CollectionDeleteOneOptions options = new CollectionDeleteOneOptions()
.sort(Sort.vector(new float[] {0.12f, 0.52f, 0.32f}));
CollectionDeleteResult result = collection.deleteOne(filter, options);
System.out.println(result.getDeletedCount());
}
}
You can provide the search vector as an array of floats, or you can use $binary
to provide the search vector as a Base64-encoded string.
$binary
can be more performant.
-
Array of floats
-
$binary
curl -sS -L -X POST "ASTRA_DB_API_ENDPOINT/api/json/v1/ASTRA_DB_KEYSPACE/ASTRA_DB_COLLECTION" \
--header "Token: ASTRA_DB_APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"deleteOne": {
"filter": {},
"sort": {
"$vector": [.12, .52, .32]
}
}
}'
curl -sS -L -X POST "ASTRA_DB_API_ENDPOINT/api/json/v1/ASTRA_DB_KEYSPACE/ASTRA_DB_COLLECTION" \
--header "Token: ASTRA_DB_APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"deleteOne": {
"filter": {},
"sort": {
"$vector": {"$binary": "PfXCjz8FHrg+o9cK"}
}
}
}'
Delete a document that is most similar to a search string
To find the document whose $vector
value is most similar to the $vector
value of a given search string, use a sort with the search string that you want to vectorize and match. For more information, see Find data with vector search.
Vector search with vectorize is only available for collections that have vectorize enabled. For more information, see $vector and $vectorize in collections.
-
Python
-
TypeScript
-
Java
-
curl
from astrapy import DataAPIClient
# Get an existing collection
client = DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
database = client.get_database("ASTRA_DB_API_ENDPOINT")
collection = database.get_collection("COLLECTION_NAME")
# Delete a document
result = collection.delete_one(
{},
sort={"$vectorize": "Text to vectorize"}
)
print(result)
import { DataAPIClient } from '@datastax/astra-db-ts';
// Get an existing collection
const client = new DataAPIClient('ASTRA_DB_APPLICATION_TOKEN');
const database = client.db('ASTRA_DB_API_ENDPOINT');
const collection = database.collection('COLLECTION_NAME');
// Delete a document
(async function () {
const result = await collection.deleteOne(
{},
{ sort: { $vectorize: "Text to vectorize" } }
);
console.log(result);
})();
package com.examples;
import com.datastax.astra.client.collections.Collection;
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.collections.definition.documents.Document;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.collections.commands.options.CollectionDeleteOneOptions;
import com.datastax.astra.client.collections.commands.results.CollectionDeleteResult;
import com.datastax.astra.client.core.query.Sort;
public class DeleteOne {
public static void main(String[] args) {
// Get an existing collection
Collection<Document> collection = new DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
.getDatabase("ASTRA_DB_API_ENDPOINT")
.getCollection("COLLECTION_NAME");
// Delete a document
Filter filter = new Filter();
CollectionDeleteOneOptions options = new CollectionDeleteOneOptions()
.sort(Sort.vectorize("Text to vectorize"));
CollectionDeleteResult result = collection.deleteOne(filter, options);
System.out.println(result.getDeletedCount());
}
}
curl -sS -L -X POST "ASTRA_DB_API_ENDPOINT/api/json/v1/ASTRA_DB_KEYSPACE/ASTRA_DB_COLLECTION" \
--header "Token: ASTRA_DB_APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"deleteOne": {
"filter": {},
"sort": { "$vectorize": "Text to vectorize" }
}
}'
Delete a document after applying a non-vector sort
You can use a sort clause to sort documents by one or more fields.
For more information, see Sort clauses for collections.
Sort clauses can use only indexed fields. If you apply selective indexing when you create a collection, you can’t reference non-indexed fields in sort queries.
-
Python
-
TypeScript
-
Java
-
curl
from astrapy import DataAPIClient
from astrapy.constants import SortMode
# Get an existing collection
client = DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
database = client.get_database("ASTRA_DB_API_ENDPOINT")
collection = database.get_collection("COLLECTION_NAME")
# Find a document
result = collection.delete_one(
{"metadata.language": "English"},
sort={
"rating": SortMode.ASCENDING,
"title": SortMode.DESCENDING,
}
)
print(result)
import { DataAPIClient } from '@datastax/astra-db-ts';
// Get an existing collection
const client = new DataAPIClient('ASTRA_DB_APPLICATION_TOKEN');
const database = client.db('ASTRA_DB_API_ENDPOINT');
const collection = database.collection('COLLECTION_NAME');
// Find a document
(async function () {
const result = await collection.deleteOne(
{ "metadata.language": "English" },
{ sort: {
rating: 1, // ascending
title: -1 // descending
} }
);
console.log(result);
})();
package com.examples;
import com.datastax.astra.client.collections.Collection;
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.collections.definition.documents.Document;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.collections.commands.options.CollectionDeleteOneOptions;
import com.datastax.astra.client.core.query.Sort;
public class DeleteOne {
public static void main(String[] args) {
// Get an existing collection
Collection<Document> collection = new DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
.getDatabase("ASTRA_DB_API_ENDPOINT")
.getCollection("COLLECTION_NAME");
// Delete a document
Filter filter = Filters.and(
Filters.eq("isCheckedOut", false),
Filters.lt("numberOfPages", 300));
CollectionDeleteOneOptions options = new CollectionDeleteOneOptions()
.sort(Sort.ascending("rating"), Sort.descending("title"));
CollectionDeleteResult result = collection.deleteOne(filter, options);
System.out.println(result.getDeletedCount());
}
}
curl -sS -L -X POST "ASTRA_DB_API_ENDPOINT/api/json/v1/ASTRA_DB_KEYSPACE/ASTRA_DB_COLLECTION" \
--header "Token: ASTRA_DB_APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"deleteOne": {
"filter": { "metadata.language": "English" },
"sort": {
"rating": 1,
"title": -1
}
}
}'
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.