This topic describes how to use the Java SDK to delete documents from a collection by specifying one or more IDs.
If a specified ID does not exist, the delete operation for that ID has no effect.
Prerequisites
You have created a cluster. For more information, see Create a cluster.
You have obtained an API key. For more information, see Manage API keys.
You have installed the latest version of the SDK. For more information, see Install the DashVector SDK.
API definition
// class DashVectorCollection
// Sync method
public Response<List<DocOpResult>> delete(DeleteDocRequest deleteDocRequest);
// Async method
public ListenableFuture<Response<List<DocOpResult>>> deleteAsync(DeleteDocRequest deleteDocRequest);Examples
To run the code, replace YOUR_API_KEY with your API key and YOUR_CLUSTER_ENDPOINT with your cluster endpoint.
Before you run this example, create a collection named
quickstart. For more information, see Create a collection - Examples. Then, insert data into the collection. For more information, see Insert a doc.
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 request to delete the doc.
Response<List<DocOpResult>> response = collection.delete(request);
}
}
Request parameters
Use DeleteDocRequestBuilder to construct a DeleteDocRequest object. The following table describes the available methods.
Method | Required | Default | Description |
ids(List<String> ids) | Yes | - | Document primary keys |
id(String id) | No | - | |
partition(String partition) | No | default | The partition name. |
deleteAll(Boolean deleteAll) | No | false | Specifies whether to purge all data in the partition. If you set this parameter to |
build() | - | - | Constructs a |
Response parameters
The operation returns a Response<List<DocOpResult>> object. This object contains information about the result of the operation, as described in the following table.
Method | Type | Description | Example |
getCode() | int | The return value. 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 |
getOutput() | List<DocOpResult> | The result of the delete operation. | |
getUsage() | For a doc deletion request on a collection in a serverless (pay-as-you-go) instance, this parameter indicates the number of write request units (WRUs) consumed if the request is successful. | ||
isSuccess() | Boolean | Indicates whether the request was successful. | true |