Deleting Objects

Last updated: 2021-08-11 15:29:10

    Overview

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

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

    SDK API Reference

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

    Deleting an Object

    Feature description

    This API (DELETE Object) is used to delete a specified object from a bucket.

    Sample code 1: deleting a single object

    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 "video/xxx/movie.mp4"
    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 the complete sample, go to 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 "video/xxx/movie.mp4"
    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 the complete sample, go to GitHub.

    Sample code 2: deleting a directory

    Objective-C

    QCloudGetBucketRequest* request = [QCloudGetBucketRequest new];
    // Bucket name in the format: `BucketName-APPID`
    request.bucket = @"examplebucket-1250000000";
    // Maximum number of entries to return at a time. The default value is 1,000
    request.maxKeys = 100;
    // Name of the directory to delete, prefixed with a slash (/)
    request.prefix = @"prefix";
    [request setFinishBlock:^(QCloudListBucketResult * result, NSError* error) {
       if(!error){
           NSMutableArray *deleteInfosArr = [NSMutableArray array];
           for (QCloudBucketContents *content in result.contents) {
               QCloudDeleteMultipleObjectRequest *delteRequest = [QCloudDeleteMultipleObjectRequest new];
               delteRequest.bucket = request.bucket;
                QCloudDeleteObjectInfo *object = [QCloudDeleteObjectInfo new];
               object.key = content.key;
               [deleteInfosArr addObject:object];
           }
    
           QCloudDeleteInfo *deleteInfos = [QCloudDeleteInfo new];
           deleteInfos.objects = [deleteInfosArr copy];
    
           QCloudDeleteMultipleObjectRequest *delteRequest = [QCloudDeleteMultipleObjectRequest new];
           delteRequest.bucket = @"examplebucket-1250000000";
           delteRequest.deleteObjects = deleteInfos;
           [delteRequest setFinishBlock:^(QCloudDeleteResult *outputObject, NSError *error) {
               NSLog(@"outputObject = %@",outputObject);
           }];
            [[QCloudCOSXMLService defaultCOSXML] DeleteMultipleObject:delteRequest];
    
       }
    }];
    [[QCloudCOSXMLService defaultCOSXML] GetBucket:request];
    
    Note:

    For the complete sample, go to GitHub.

    Swift

    let getBucketReq = QCloudGetBucketRequest.init();
    
    // Bucket name in the format: `BucketName-APPID`
    getBucketReq.bucket = "examplebucket-1250000000";
    
    // Maximum number of entries to return at a time. The default value is 1,000
    getBucketReq.maxKeys = 100;
    
    // Name of the directory to delete, prefixed with a slash (/)
    getBucketReq.prefix = "dir/";
    
    getBucketReq.setFinish { (result, error) in
       if let result = result {
           let contents = result.contents;
           let infos = NSMutableArray.init();
           for content in contents {
               let info = QCloudDeleteObjectInfo.init();
               info.key = content.key;
               infos.add(info);
           }
           let mutipleDel = QCloudDeleteMultipleObjectRequest.init();
           // File set to be deleted
           let deleteInfos = QCloudDeleteInfo.init();
           // Bucket name in the format: `BucketName-APPID`
           mutipleDel.bucket = "examplebucket-1250000000";
    
           deleteInfos.objects = infos as! [QCloudDeleteObjectInfo];
    
           // Boolean value. Determines whether to enable the `Quiet` mode:
           // true: enable the `Quiet` mode
           // false: enable the `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);
       } else {
           print(error!);
       }
    }
    QCloudCOSXMLService.defaultCOSXML().getBucket(getBucketReq);
    
    Note:

    For the complete sample, go to GitHub.

    Deleting Multiple Objects

    Feature description

    The API (DELETE Multiple Objects) is used to delete multiple objects.

    Sample code 1: deleting multiple objects

    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 "video/xxx/movie.mp4"
    deletedObject0.key = @"exampleobject";
    // File set to be deleted
    QCloudDeleteInfo* deleteInfo = [QCloudDeleteInfo new];
    // Boolean value. Determines whether to enable the `Quiet` mode:
    // true: enable the `Quiet` mode
    // false: enable the `Verbose` mode
    // Default value: false
    deleteInfo.quiet = NO;
    // Array that stores the information of the objects to be deleted
    deleteInfo.objects = @[deletedObject0];
    // Encapsulates the information of the multiple objects to be deleted in batches
    delteRequest.deleteObjects = deleteInfo;
    [delteRequest setFinishBlock:^(QCloudDeleteResult* outputObject,
                                  NSError *error) {
       // outputObject contains information such as the ETag or custom headers in the response.
    
    }];
    [[QCloudCOSXMLService defaultCOSXML] DeleteMultipleObject:delteRequest];
    
    Note:

    For the complete sample, go to 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 "video/xxx/movie.mp4"
    info1.key = "exampleobject";
    let info2 = QCloudDeleteObjectInfo.init();
    // File set to be deleted
    let deleteInfos = QCloudDeleteInfo.init();
    // Array that stores the information of the objects to be deleted
    deleteInfos.objects = [info1,info2];
    // Boolean value. Determines whether to enable the `Quiet` mode:
    // true: enable the `Quiet` mode
    // false: enable the `Verbose` mode
    // Default value: false
    deleteInfos.quiet = false;
    // Encapsulates the information of 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 the complete sample, go to GitHub.

    Sample code 2: deleting objects with a specified prefix

    Objective-C

    QCloudGetBucketRequest* request = [QCloudGetBucketRequest new];
    // Bucket name in the format: `BucketName-APPID`
    request.bucket = @"examplebucket-1250000000";
    // Maximum number of entries to return at a time. The default value is 1,000
    request.maxKeys = 100;
    //To delete files with a specified prefix, set `prefix` to the prefix of the files to delete
    request.prefix = @"prefix";
    [request setFinishBlock:^(QCloudListBucketResult * result, NSError* error) {
       if(!error){
           NSMutableArray *deleteInfosArr = [NSMutableArray array];
           for (QCloudBucketContents *content in result.contents) {
               QCloudDeleteMultipleObjectRequest *delteRequest = [QCloudDeleteMultipleObjectRequest new];
               delteRequest.bucket = request.bucket;
                QCloudDeleteObjectInfo *object = [QCloudDeleteObjectInfo new];
               object.key = content.key;
               [deleteInfosArr addObject:object];
           }
    
           QCloudDeleteInfo *deleteInfos = [QCloudDeleteInfo new];
           deleteInfos.objects = [deleteInfosArr copy];
    
           QCloudDeleteMultipleObjectRequest *delteRequest = [QCloudDeleteMultipleObjectRequest new];
           delteRequest.bucket = @"examplebucket-1250000000";
           delteRequest.deleteObjects = deleteInfos;
           [delteRequest setFinishBlock:^(QCloudDeleteResult *outputObject, NSError *error) {
               NSLog(@"outputObject = %@",outputObject);
           }];
            [[QCloudCOSXMLService defaultCOSXML] DeleteMultipleObject:delteRequest];
    
       }
    }];
    [[QCloudCOSXMLService defaultCOSXML] GetBucket:request];
    
    Note:

    For the complete sample, go to GitHub.

    Swift

    let getBucketReq = QCloudGetBucketRequest.init();
    
    // Bucket name in the format: `BucketName-APPID`
    getBucketReq.bucket = "examplebucket-1250000000";
    
    // Maximum number of entries to return at a time. The default value is 1,000
    getBucketReq.maxKeys = 100;
    
    //To delete files with a specified prefix, set `prefix` to the prefix of the files to delete
    getBucketReq.prefix = "dir/";
    
    getBucketReq.setFinish { (result, error) in
       if let result = result {
           let contents = result.contents;
           let infos = NSMutableArray.init();
           for content in contents {
               let info = QCloudDeleteObjectInfo.init();
               info.key = content.key;
               infos.add(info);
           }
           let mutipleDel = QCloudDeleteMultipleObjectRequest.init();
           // File set to be deleted
           let deleteInfos = QCloudDeleteInfo.init();
           // Bucket name in the format: `BucketName-APPID`
           mutipleDel.bucket = "examplebucket-1250000000";
    
           deleteInfos.objects = infos as! [QCloudDeleteObjectInfo];
    
           // Boolean value. Determines whether to enable the `Quiet` mode:
           // true: enable the `Quiet` mode
           // false: enable the `Verbose` mode
           // Default value: false
           deleteInfos.quiet = false;
    
           // Encapsulates the information of 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);
       } else {
           print(error!);
       }
    }
    QCloudCOSXMLService.defaultCOSXML().getBucket(getBucketReq);
    
    Note:

    For the complete sample, go to GitHub.