Deleting Objects
If you have any questions during development, post them on the Issues page of GitHub. For details about parameters and usage of each API, see the API Reference
Exercise caution when performing this operation. If the versioning function is disabled for the bucket where the object is located, the object cannot be restored after being deleted.
Deleting a Single Object
You can call ObsClient.deleteObject to delete a single object. Sample code is as follows:
String endPoint = "https://your-endpoint";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";
// Create an instance of ObsClient.
ObsClient obsClient = new ObsClient(ak, sk, endPoint);
obsClient.deleteObject("bucketname", "objectname");
Deleting Objects in a Batch
You can call ObsClient.deleteObjects to delete objects in a batch.
The following table describes the parameters involved in this API.
|
Parameter |
Description |
Method in OBS Java SDK |
|---|---|---|
|
bucketName |
Bucket name |
DeleteObjectsRequest.setBucketName |
|
quiet |
Response mode of a batch deletion request. If this field is set to false, objects involved in the deletion will be returned. If this field is set to true, only objects failed to be deleted will be returned. |
DeleteObjectsRequest.setQuiet |
A maximum of 1,000 objects can be deleted each time. Two response modes are supported: verbose (detailed) and quiet (brief).
- In verbose mode (default mode), the returned response includes the deletion result of each requested object.
- In quiet mode, the returned response includes only results of objects failed to be deleted.
Sample code:
String endPoint = "https://your-endpoint";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";
ObsClient obsClient = new ObsClient(ak, sk, endPoint);
ListVersionsRequest request = new ListVersionsRequest("bucketname");
// Delete 100 objects at a time.
request.setMaxKeys(100);
ListVersionsResult result;
do {
result = obsClient.listVersions(request);
DeleteObjectsRequest deleteRequest = new DeleteObjectsRequest("bucketname");
for(VersionOrDeleteMarker v : result.getVersions()) {
deleteRequest.addKeyAndVersion(v.getKey(), v.getVersionId());
}
DeleteObjectsResult deleteResult = obsClient.deleteObjects(deleteRequest);
// Obtain the list of successfully deleted objects.
System.out.println(deleteResult.getDeletedObjectResults());
// Obtain the list of objects failed to be deleted.
System.out.println(deleteResult.getErrorResults());
request.setKeyMarker(result.getNextKeyMarker());
request.setVersionIdMarker(result.getNextVersionIdMarker());
}while(result.isTruncated());
Last Article: Listing Objects
Next Article: Copying an Object
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.