This topic describes how to delete a single object or delete multiple objects at a time.

Warning Deleted objects cannot be recovered. Exercise caution when you delete objects.

Usage notes

To delete an object, you must have write permissions on the bucket in which the object is stored.

Delete a single object

The following code provides an example on how to delete an object named exampleobject.txt from a bucket named examplebucket:

// Create the delete request. 
// Specify the bucket name such as examplebucket and the full path of the object such as exampledir/exampleobject.txt. The full path cannot contain the bucket name. 
DeleteObjectRequest delete = new DeleteObjectRequest("examplebucket", "exampledir/exampleobject.txt");
// Asynchronously delete the object. 
OSSAsyncTask deleteTask = oss.asyncDeleteObject(delete, new OSSCompletedCallback<DeleteObjectRequest, DeleteObjectResult>() {
    @Override
    public void onSuccess(DeleteObjectRequest request, DeleteObjectResult result) {
        Log.d("asyncDeleteObject", "success!");
    }

    @Override
    public void onFailure(DeleteObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
        // Handle request exceptions. 
        if (clientExcepion != null) {
            // Handle client-side exceptions such as network errors. 
            clientExcepion.printStackTrace();
        }
        if (serviceException != null) {
            // Handle server-side exceptions. 
            Log.e("ErrorCode", serviceException.getErrorCode());
            Log.e("RequestId", serviceException.getRequestId());
            Log.e("HostId", serviceException.getHostId());
            Log.e("RawMessage", serviceException.getRawMessage());
        }
    }
});

Delete multiple objects at a time

You can delete up to 1,000 objects at a time.

The result can be returned in the following two modes. Select a return mode based on your requirements.

  • verbose: If isQuiet is not specified or is set to false, a list of all deleted objects is returned. This is the default return mode.
  • quiet: If isQuiet is set to true, a list of objects that failed to be deleted is returned.

The following code provides an example on how to delete multiple specified objects from a bucket named examplebucket and return the result in the quiet mode:

// Specify the full paths of the objects that you want to delete. Do not include the bucket name in the full paths of the objects. 
List<String> objectKeys = new ArrayList<String>();
objectKeys.add("exampleobject.txt");
objectKeys.add("testfolder/sampleobject.txt");

// Set isQuiet to true to return only a list of objects that failed to be deleted. 
DeleteMultipleObjectRequest request = new DeleteMultipleObjectRequest("examplebucket", objectKeys, true);

mOss.asyncDeleteMultipleObject(request, new OSSCompletedCallback<DeleteMultipleObjectRequest, DeleteMultipleObjectResult>() {
    @Override
    public void onSuccess(DeleteMultipleObjectRequest request, DeleteMultipleObjectResult result) {
        Log.i("DeleteMultipleObject", "success");
    }

    @Override
    public void onFailure(DeleteMultipleObjectRequest request, ClientException clientException, ServiceException serviceException) {
        // Handle request exceptions. 
        if (clientException != null) {
            // Handle client-side exceptions such as network errors. 
            clientException.printStackTrace();
        }
        if (serviceException != null) {
            // Handle server-side exceptions. 
            Log.e("ErrorCode", serviceException.getErrorCode());
            Log.e("RequestId", serviceException.getRequestId());
            Log.e("HostId", serviceException.getHostId());
            Log.e("RawMessage", serviceException.getRawMessage());
        }
    }
});

References

  • Delete a single object

    For more information about the API operation that you can call to delete an object, see DeleteObject.

  • Deletes multiple objects

    For more information about the API operation that you can call to delete multiple objects, see DeleteMultipleObjects.