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 Java.
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
// The DashVectorCollection class.
// The method working in synchronous mode.
public Response<Void> delete(DeleteDocRequest deleteDocRequest);
// The method working in asynchronous mode.
public ListenableFuture<Response<Void>> deleteAsync(DeleteDocRequest deleteDocRequest);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
quickstartin 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 com.aliyun.dashvector.DashVectorClient;
import com.aliyun.dashvector.DashVectorCollection;
import com.aliyun.dashvector.common.DashVectorException;
import com.aliyun.dashvector.models.requests.DeleteDocRequest;
import com.aliyun.dashvector.models.responses.Response;
public class Main {
public static void main(String[] args) throws DashVectorException {
DashVectorClient client = new DashVectorClient("YOUR_API_KEY", "YOUR_CLUSTER_ENDPOINT");
DashVectorCollection collection = client.get("quickstart");
// Build a DeleteDocRequest object.
DeleteDocRequest request = DeleteDocRequest.builder()
.id("1")
.build();
// Send the document deletion request.
Response<Void> response = collection.delete(request);
}
}
Request parameters
You can use a DeleteDocRequestBuilder instance to build a DeleteDocRequest object. The following table describes the methods that can be called by the instance.
Method | Required | Default value | Description |
ids(List<String> ids) | Yes | - | Sets the primary key or a list of primary keys of documents. |
id(String id) | No | - | |
partition(String partition) | No | default | Sets the name of the partition. |
deleteAll(Boolean deleteAll) | No | false | Specifies whether to delete all data from the partition. If the value is |
build() | - | - | Builds a |
Response parameters
A Response<Void> object is returned, which contains the operation result, as described in the following table.
Method | Type | Description | Example |
getCode() | int | The returned status code. For more information, see Status codes. | 0 |
getMessage() | String | The returned message. | success |
getRequestId() | String | The unique ID of the request. | 19215409-ea66-4db9-8764-26ce2eb5bb99 |
isSuccess() | Boolean | Specifies whether the operation is successful. | true |