All Products
Search
Document Center

Object Storage Service:Delete an object

Last Updated:Mar 01, 2024

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

Warning

Deleted objects cannot be restored. Exercise caution when you delete objects.

Usage notes

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

  • Before you run the sample code in this topic, you must create an OSSClient instance by using methods such as using a custom domain name or Security Token Service (STS). For more information, see Initialization.

Delete a single object

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

// Create a delete request. 
// Specify the name of the bucket and the full path of the object. In this example, the bucket name is examplebucket and the full path of the object is exampledir/exampleobject.txt. Do not include the bucket name in the full path of the object. 
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 exceptions. 
            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 deletion result can be returned in the following two modes. Select a return mode based on your requirements.

  • verbose (default): If isQuiet is not specified or is set to false, a list of all deleted objects is returned.

  • quiet: If isQuiet is set to true, a list of objects that failed to be deleted is returned.

The following sample 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. 
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);

oss.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 exceptions. 
            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 a single object, see DeleteObject.

  • Delete multiple objects at a time

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

  • For more information about how to initialize an OSSClient instance, see Initialization.