• Object Storage Service

obs
  1. Help Center
  2. Object Storage Service
  3. Developer Guide (Java SDK)
  4. Object Management
  5. Deleting Objects

Deleting Objects

Deleting a Single Object

You can call ObsClient.deleteObject to delete a single object. Sample code is as follows:

String endPoint = "yourdomainname";
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", "objectkey");

// Close obsClient. 
obsClient.close();

Batch Deleting Objects

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 = "yourdomainname";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";

// Create an instance of ObsClient.
ObsClient obsClient = new ObsClient(ak, sk, endPoint);

DeleteObjectsRequest request = new DeleteObjectsRequest("bucketname");
// Set the response mode to verbose.
request.setQuiet(false);

List<KeyAndVersion> toDelete = new ArrayList<KeyAndVersion>();

toDelete.add(new KeyAndVersion("objectkey1"));
toDelete.add(new KeyAndVersion("objectkey2"));
toDelete.add(new KeyAndVersion("objectkey3"));
request.setKeyAndVersions(toDelete.toArray(new KeyAndVersion[toDelete.size()]));

DeleteObjectsResult result = obsClient.deleteObjects(request);
// Obtain the list of successfully deleted objects.
System.out.println(result.getDeletedObjectResults());
// Obtain the list of objects failed to be deleted.
System.out.println(result.getErrorResults());

// Close obsClient. 
obsClient.close();