Deleting an Object

Last updated: 2020-09-10 14:44:53

    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 from a bucket.

    Sample code

    Objective-C

    QCloudDeleteObjectRequest* deleteObjectRequest = [QCloudDeleteObjectRequest new];
    
    // Bucket name in the format: `BucketName-APPID`
    deleteObjectRequest.bucket = @"examplebucket-1250000000";
    
    // Object key, i.e. the full path of a COS object. If the object is in a directory, the path should be "dir1/object1"
    deleteObjectRequest.object = @"exampleobject";
    
    [deleteObjectRequest setFinishBlock:^(id outputObject, NSError *error) {
        // `outputObject` contains all the HTTP response headers
        NSDictionary* info = (NSDictionary *) outputObject;
    }];
    
    [[QCloudCOSXMLService defaultCOSXML] DeleteObject:deleteObjectRequest];

    Note:

    For more samples, please visit GitHub.

    Swift

    let deleteObject = QCloudDeleteObjectRequest.init();
    
    // Bucket name in the format: `BucketName-APPID`
    deleteObject.bucket = "examplebucket-1250000000";
    
    // Object key, i.e. the full path of a COS object. If the object is in a directory, the path should be "dir1/object1"
    deleteObject.object = "exampleobject";
    
    deleteObject.finishBlock = {(result,error)in
        if let result = result {
            // "result" contains response headers
        } else {
            print(error!);
        }
    }
    QCloudCOSXMLService.defaultCOSXML().deleteObject(deleteObject);

    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

    Objective-C

    QCloudDeleteMultipleObjectRequest* delteRequest = [QCloudDeleteMultipleObjectRequest new];
    delteRequest.bucket = @"examplebucket-1250000000";
    
    // Single file to be deleted
    QCloudDeleteObjectInfo* deletedObject0 = [QCloudDeleteObjectInfo new];
    
    // Object key, i.e. the full path of a COS object. If the object is in a directory, the path should be "dir1/object1"
    deletedObject0.key = @"exampleobject";
    
    // File set to be deleted
    QCloudDeleteInfo* deleteInfo = [QCloudDeleteInfo new];
    
    // Boolean value; Determines whether to enable `Quiet` mode:
    // true: enables `Quiet` mode
    // false: enables `Verbose` mode
    // Default value: false
    deleteInfo.quiet = NO;
    
    // The array that stores the information on the objects to be deleted
    deleteInfo.objects = @[deletedObject0];
    
    // Encapsulates the information on the multiple objects to be deleted in batches
    delteRequest.deleteObjects = deleteInfo;
    
    [delteRequest setFinishBlock:^(QCloudDeleteResult* outputObject,
                                   NSError *error) {
        // outputObject returns information such as the Etag or custom headers in the response 
    
    }];
    
    [[QCloudCOSXMLService defaultCOSXML] DeleteMultipleObject:delteRequest];

    Note:

    For more samples, please visit GitHub.

    Swift

    let mutipleDel = QCloudDeleteMultipleObjectRequest.init();
    
    // Bucket name in the format: `BucketName-APPID`
    mutipleDel.bucket = "examplebucket-1250000000";
    
    // Single file to be deleted
    let info1 = QCloudDeleteObjectInfo.init();
    
    // Object key, i.e. the full path of a COS object. If the object is in a directory, the path should be "dir1/object1"
    info1.key = "exampleobject";
    
    let info2 = QCloudDeleteObjectInfo.init();
    
    // File set to be deleted
    let deleteInfos = QCloudDeleteInfo.init();
    
    // The array that stores the information on the objects to be deleted
    deleteInfos.objects = [info1,info2];
    
    // Boolean value; Determines whether to enable `Quiet` mode:
    // true: enables `Quiet` mode
    // false: enables `Verbose` mode
    // Default value: false
    deleteInfos.quiet = false;
    
    // Encapsulates the information on the multiple objects to be deleted in batches
    mutipleDel.deleteObjects = deleteInfos;
    
    mutipleDel.setFinish { (result, error) in
        if let result = result {
            let deleted = result.deletedObjects
            let failed = result.deletedFailedObjects
        } else {
            print(error!);
        }
    }
    QCloudCOSXMLService.defaultCOSXML().deleteMultipleObject(mutipleDel);

    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