Deleting an Object

Last updated: 2020-08-18 10:55:27

    Overview

    This document provides an overview of APIs and SDK code samples related to object deletion.

    API Operation Name Description
    DELETE Object Deleting a single object Deletes a specified object from a bucket
    DELETE Multiple Object Deleting multiple objects Deletes multiple objects from a bucket in a single request

    SDK API Reference

    For the parameters and method descriptions of all the APIs in the SDK, please see SDK API Reference.

    Deleting a Single Object

    Feature description

    This API is used to delete a specified object.

    Sample code

    String bucket = "examplebucket-1250000000"; // Bucket name in the format: `BucketName-APPID`
    String cosPath = "exampleobject"; // Location identifier of the object in the bucket, i.e., the object key
    
    DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(bucket,
            cosPath);
    cosXmlService.deleteObjectAsync(deleteObjectRequest,
            new CosXmlResultListener() {
        @Override
        public void onSuccess(CosXmlRequest cosXmlRequest, CosXmlResult result) {
            DeleteObjectResult deleteObjectResult = (DeleteObjectResult) result;
        }
    
        @Override
        public void onFail(CosXmlRequest cosXmlRequest,
                           CosXmlClientException clientException,
                           CosXmlServiceException serviceException) {
            if (clientException != null) {
                clientException.printStackTrace();
            } else {
                serviceException.printStackTrace();
            }
        }
    });

    Note:

    For more samples, please visit GitHub.

    Deleting Multiple Objects

    Feature description

    This API is used to delete multiple objects in a single request.

    Sample code

    String bucket = "examplebucket-1250000000"; // Bucket name in the format: `BucketName-APPID`
    List<String> objectList = new ArrayList<String>();
    objectList.add("exampleobject1"); // Location identifier of the object in the bucket, i.e., the object key
    objectList.add("exampleobject2"); // Location identifier of the object in the bucket, i.e., the object key
    
    DeleteMultiObjectRequest deleteMultiObjectRequest =
            new DeleteMultiObjectRequest(bucket, objectList);
    // In quiet mode, only information on objects that failed to be deleted will be returned; otherwise, the deletion result of each object will be returned.
    deleteMultiObjectRequest.setQuiet(true);
    cosXmlService.deleteMultiObjectAsync(deleteMultiObjectRequest,
            new CosXmlResultListener() {
        @Override
        public void onSuccess(CosXmlRequest cosXmlRequest, CosXmlResult result) {
            DeleteMultiObjectResult deleteMultiObjectResult =
                    (DeleteMultiObjectResult) result;
        }
    
        @Override
        public void onFail(CosXmlRequest cosXmlRequest,
                           CosXmlClientException clientException,
                           CosXmlServiceException serviceException) {
            if (clientException != null) {
                clientException.printStackTrace();
            } else {
                serviceException.printStackTrace();
            }
        }
    });

    Note:

    For more samples, please visit GitHub.

    Was this page helpful?

    Was this page helpful?

    • Not at all
    • Not very helpful
    • Somewhat helpful
    • Very helpful
    • Extremely helpful
    Send Feedback
    Help