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.

A maximum of 1000 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 successfully deleted objects.
       Log.i("DeletesObjects",deleteResult.getDeletedObjectResults());
       // Obtain the list of objects failed to be deleted.
       Log.i("DeletesObjects", deleteResult.getErrorResults());
       
       request.setKeyMarker(deleteResult.getNextKeyMarker());
       request.setVersionIdMarker(deleteResult.getNextVersionIdMarker());
}while(result.isTruncated());