This topic describes how to delete one or more documents from a collection based on the document ID or ID list by using the SDK for Python.
If the ID of a document to be deleted does not exist, the delete operation is invalid for the document.
Prerequisites
A cluster is created. For more information, see Create a cluster.
An API key is obtained. For more information, see Manage API keys.
The SDK of the latest version is installed. For more information, see Install DashVector SDK.
API definition
Collection.delete(
ids: Union[str, List[str]],
partition: Optional[str] = None,
async_req: bool = False,
delete_all: bool = False
) -> DashVectorResponse
Example
You need to replace YOUR_API_KEY with your API key and YOUR_CLUSTER_ENDPOINT with the endpoint of your cluster in the sample code for the code to run properly.
You need to create a collection named
quickstart
in advance. For more information, see the "Example" section of the Create a collection topic. You also need to insert some documents in advance. For more information, see Insert documents.
import dashvector
client = dashvector.Client(
api_key='YOUR_API_KEY',
endpoint='YOUR_CLUSTER_ENDPOINT'
)
collection = client.get(name='quickstart')
ret = collection.delete('1')
# Check whether the delete method is successfully called.
if ret:
print('delete success')
# Delete multiple documents at a time.
ret = collection.delete(['1','2'])
# Delete all data from the partition.
ret = collection.delete(delete_all=True)
Request parameters
Parameter | Type | Default value | Description |
ids | str or List[str] | - | The primary key or primary key list. |
partition | Optional[str] | None | Optional. The name of the partition. |
async_req | bool | False | Optional. Specifies whether to enable the asynchronous mode. |
delete_all | bool | False | Optional. Specifies whether to delete all data from the partition. If the value is True, the ids parameter must be left empty. |
Response parameters
A DashVectorResponse object is returned, which contains the operation result, as described in the following table.
Parameter | Type | Description | Example |
code | int | The returned status code. For more information, see Status codes. | 0 |
message | str | The returned message. | success |
request_id | str | The unique ID of the request. | 19215409-ea66-4db9-8764-26ce2eb5bb99 |