tencent cloud

Feedback

Deleting Objects

Last updated: 2022-04-26 16:12:29

    Overview

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

    API Operation Description
    DELETE Object Deleting an object Deletes an object from a bucket.
    DELETE Multiple Objects Deleting multiple objects Deletes multiple objects from a bucket.

    Deleting a Single Object

    Description

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

    Method prototype

    delete_object(Bucket, Key, **kwargs)
    

    Sample 1. Deleting an object

    # -*- coding=utf-8
    from qcloud_cos import CosConfig
    from qcloud_cos import CosS3Client
    import sys
    import logging
    # In most cases, set the log level to INFO. If you need to debug, you can set it to DEBUG and the SDK will print the communication information of the client.
    logging.basicConfig(level=logging.INFO, stream=sys.stdout)
    # 1. Set user attributes such as secret_id, secret_key, and region. Appid has been removed from CosConfig and thus needs to be specified in Bucket, which is formatted as BucketName-Appid.
    secret_id = 'SecretId'     # Replace it with the actual SecretId, which can be viewed and managed at https://console.intl.cloud.tencent.com/cam/capi
    secret_key = 'SecretKey'     # Replace it with the actual SecretKey, which can be viewed and managed at https://console.intl.cloud.tencent.com/cam/capi
    region = 'ap-beijing'      # Replace it with the actual region, which can be viewed in the console at https://console.intl.cloud.tencent.com/cos5/bucket
                             # For the list of regions supported by COS, see https://intl.cloud.tencent.com/document/product/436/6224
    token = None               # Token is required for temporary keys but not permanent keys. For more information about how to generate and use a temporary key, visit https://intl.cloud.tencent.com/document/product/436/14048
    scheme = 'https'           # Specify whether to use HTTP or HTTPS protocol to access COS. This field is optional and is `https` by default
    config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)
    client = CosS3Client(config)
    response = client.delete_object(
      Bucket='examplebucket-1250000000',
      Key='exampleobject'
    )
    

    Sample 2. Deleting a directory

    In COS, a directory is an object whose path ends with a slash (/). Therefore, you can call the Delete Object API to delete a directory.

    # -*- coding=utf-8
    from qcloud_cos import CosConfig
    from qcloud_cos import CosS3Client
    import sys
    import logging
    # In most cases, set the log level to INFO. If you need to debug, you can set it to DEBUG and the SDK will print the communication information of the client.
    logging.basicConfig(level=logging.INFO, stream=sys.stdout)
    # 1. Set user attributes such as secret_id, secret_key, and region. Appid has been removed from CosConfig and thus needs to be specified in Bucket, which is formatted as BucketName-Appid.
    secret_id = 'SecretId'     # Replace it with the actual SecretId, which can be viewed and managed at https://console.intl.cloud.tencent.com/cam/capi
    secret_key = 'SecretKey'     # Replace it with the actual SecretKey, which can be viewed and managed at https://console.intl.cloud.tencent.com/cam/capi
    region = 'ap-beijing'      # Replace it with the actual region, which can be viewed in the console at https://console.intl.cloud.tencent.com/cos5/bucket
                             # For the list of regions supported by COS, see https://intl.cloud.tencent.com/document/product/436/6224
    token = None               # Token is required for temporary keys but not permanent keys. For more information about how to generate and use a temporary key, visit https://intl.cloud.tencent.com/document/product/436/14048
    scheme = 'https'           # Specify whether to use HTTP or HTTPS protocol to access COS. This field is optional and is `https` by default
    config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)
    client = CosS3Client(config)
    to_delete_dir='path/to/delete/dir/'
    response = client.delete_object(
      Bucket='examplebucket-1250000000',
      Key=to_delete_dir,
    )
    print(response)
    

    Sample 3: Deleting objects with a specified prefix

    # -*- coding=utf-8
    from qcloud_cos import CosConfig
    from qcloud_cos import CosS3Client
    import sys
    import logging
    # In most cases, set the log level to INFO. If you need to debug, you can set it to DEBUG and the SDK will print the communication information of the client.
    logging.basicConfig(level=logging.INFO, stream=sys.stdout)
    # Set user attributes such as secret_id, secret_key, and region. Appid has been removed from CosConfig and thus needs to be specified in Bucket, which is formatted as BucketName-Appid.
    secret_id = 'SecretId'     # Replace it with the actual SecretId, which can be viewed and managed at https://console.intl.cloud.tencent.com/cam/capi
    secret_key = 'SecretKey'     # Replace it with the actual SecretKey, which can be viewed and managed at https://console.intl.cloud.tencent.com/cam/capi
    region = 'ap-beijing'      # Replace it with the actual region, which can be viewed in the console at https://console.intl.cloud.tencent.com/cos5/bucket
                             # For the list of regions supported by COS, see https://intl.cloud.tencent.com/document/product/436/6224
    token = None               # Token is required for temporary keys but not permanent keys. For more information about how to generate and use a temporary key, visit https://intl.cloud.tencent.com/document/product/436/14048
    scheme = 'https'           # Specify whether to use HTTP or HTTPS protocol to access COS. This field is optional and is `https` by default
    config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)
    client = CosS3Client(config)
    # Delete objects with the specified prefix.
    bucket = 'examplebucket-1250000000'
    is_over = False
    marker = ''
    prefix = 'root/logs'
    while not is_over:
      response = client.list_objects(Bucket=bucket, Prefix=prefix, Marker=marker)
      if response['Contents']:
          for content in response['Contents']:
              print("delete object: ", content['Key'])
              client.delete_object(Bucket=bucket, Key=content['Key'])
           if response['IsTruncated'] == 'false':
              is_over = True
              marker = response['Marker']
    

    Sample request with all parameters

    response = client.delete_object(
      Bucket='examplebucket-1250000000',
      Key='exampleobject',
      VersionId='string',
    )
    

    Parameter description

    Parameter Description Type Required
    Bucket Bucket name in the format of BucketName-APPID String Yes
    Key Object key, which uniquely identifies an object in a bucket. For example, if an object’s access endpoint is examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg, its key is doc/pic.jpg. String Yes
    VersionId Version ID of the object if versioning is enabled String No

    Response description

    This response contains information on the deleted object in dict format:

    {
      'x-cos-version-id': 'string',
      'x-cos-delete-marker': 'true'|'false',
    }
    
    Parameter Description Type
    x-cos-version-id Version ID of the deleted object String
    x-cos-delete-marker Whether the deleted object is a delete marker String

    Deleting Multiple Objects

    Description

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

    Method prototype

    delete_objects(Bucket, Delete={}, **kwargs)
    

    Sample request

    # -*- coding=utf-8
    from qcloud_cos import CosConfig
    from qcloud_cos import CosS3Client
    import sys
    import logging
    # In most cases, set the log level to INFO. If you need to debug, you can set it to DEBUG and the SDK will print the communication information of the client.
    logging.basicConfig(level=logging.INFO, stream=sys.stdout)
    # 1. Set user attributes such as secret_id, secret_key, and region. Appid has been removed from CosConfig and thus needs to be specified in Bucket, which is formatted as BucketName-Appid.
    secret_id = 'SecretId'     # Replace it with the actual SecretId, which can be viewed and managed at https://console.intl.cloud.tencent.com/cam/capi
    secret_key = 'SecretKey'     # Replace it with the actual SecretKey, which can be viewed and managed at https://console.intl.cloud.tencent.com/cam/capi
    region = 'ap-beijing'      # Replace it with the actual region, which can be viewed in the console at https://console.intl.cloud.tencent.com/cos5/bucket
                             # For the list of regions supported by COS, see https://intl.cloud.tencent.com/document/product/436/6224
    token = None               # Token is required for temporary keys but not permanent keys. For more information about how to generate and use a temporary key, visit https://intl.cloud.tencent.com/document/product/436/14048
    scheme = 'https'           # Specify whether to use HTTP or HTTPS protocol to access COS. This field is optional and is `https` by default
    config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)
    client = CosS3Client(config)
    response = client.delete_objects(
      Bucket='examplebucket-1250000000',
      Delete={
          'Object': [
              {
                  'Key': 'exampleobject1'
              },
              {
                  'Key': 'exampleobject2'
              }
          ]
      }
    )
    

    Sample request with all parameters

    response = client.delete_objects(
      Bucket='examplebucket-1250000000',
      Delete={
          'Object': [
              {
                  'Key': 'exampleobject1',
                  'VersionId': 'string'
              },
              {
                  'Key': 'exampleobject2',
                  'VersionId': 'string'
              },
          ],
          'Quiet': 'true'|'false'
      }
    )
    

    Parameter description

    Parameter Description Type Required
    Bucket Bucket name in the format of BucketName-APPID String Yes
    Delete Response method and target objects to delete Dict Yes
    Objects Information of each object to delete List Yes
    Key Object key, which uniquely identifies an object in a bucket. For example, if an object’s access endpoint is examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg, its key is doc/pic.jpg. String Yes
    VersionId Version ID of the target object if versioning is enabled String No
    Quiet Response method. Valid values: true: returns only the failed results; false (default): returns all results. String No

    Response description

    The response contains the deletion results in dict format:

    {
       'Deleted': [
           {
               'Key': 'string',
               'VersionId': 'string',
               'DeleteMarker': 'true'|'false',
               'DeleteMarkerVersionId': 'string'
           },
           {
               'Key': 'string',
           },
       ],
       'Error': [
           {
               'Key': 'string',
               'VersionId': 'string',
               'Code': 'string',
               'Message': 'string'
           },
       ]
    }
    
    Parameter Description Type
    Deleted Information on the successfully deleted objects List
    Key Paths to the successfully deleted objects String
    VersionId Version IDs of the successfully deleted objects String
    DeleteMarker Whether a successfully deleted object is a delete marker String
    DeleteMarkerVersionId Version IDs of the delete markers of the successfully deleted objects String
    Error Information on the objects that failed to be deleted List
    Key Paths to the objects that failed to be deleted String
    VersionId Version IDs of the objects that failed to be deleted String
    Code Error codes for the deletion failures String
    Message Error messages for the deletion failures String

    Deleting Multiple Objects (Deleting a Directory)

    Description

    COS does not have the concept of directories, but you can use slashes (/) as the delimiter to simulate directories.

    In COS, deleting a directory and the objects contained actually means deleting objects that have the same specified prefix. Currently, COS’s Python SDK does not provide a standalone API to perform this operation. However, you can still do so with a combination of basic operations (query object list + batch delete objects).

    Sample request

    # -*- coding=utf-8
    from qcloud_cos import CosConfig
    from qcloud_cos import CosS3Client
    from qcloud_cos.cos_threadpool import SimpleThreadPool
    import os
    import sys
    import logging
    # In most cases, set the log level to INFO. If you need to debug, you can set it to DEBUG and the SDK will print the communication information of the client.
    logging.basicConfig(level=logging.INFO, stream=sys.stdout)
    # 1. Set user attributes such as secret_id, secret_key, and region. Appid has been removed from CosConfig and thus needs to be specified in Bucket, which is formatted as BucketName-Appid.
    secret_id = 'SecretId'     # Replace it with the actual SecretId, which can be viewed and managed at https://console.intl.cloud.tencent.com/cam/capi
    secret_key = 'SecretKey'     # Replace it with the actual SecretKey, which can be viewed and managed at https://console.intl.cloud.tencent.com/cam/capi
    region = 'ap-beijing'      # Replace it with the actual region, which can be viewed in the console at https://console.intl.cloud.tencent.com/cos5/bucket
                              # For the list of regions supported by COS, see https://intl.cloud.tencent.com/document/product/436/6224
    token = None               # Token is required for temporary keys but not permanent keys. For more information about how to generate and use a temporary key, visit https://intl.cloud.tencent.com/document/product/436/14048
    scheme = 'https'           # Specify whether to use HTTP or HTTPS protocol to access COS. This field is optional and is `https` by default
    config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)
    client = CosS3Client(config)
    bucket = 'examplebucket-1250000000'
    folder = 'folder/' # A directory to delete (an object name ending with a slash (/) is a directory)
    def delete_cos_dir():
       pool = SimpleThreadPool()
       marker = ""
       while True:
           file_infos = []
            # List 100 objects in a response.
           response = client.list_objects(Bucket=bucket, Prefix=folder, Marker=marker, MaxKeys=100)
            if "Contents" in response:
               contents = response.get("Contents")
               file_infos.extend(contents)
               pool.add_task(delete_files, file_infos)
            # Quit after the listing.
           if response['IsTruncated'] == 'false':
               break
           
           # Get the next response.
           marker = response["NextMarker"]
        pool.wait_completion()
       return None   
    def delete_files(file_infos):
        # Construct the batch delete request.
       delete_list = []
       for file in file_infos:
           delete_list.append({"Key": file['Key']})
        response = client.delete_objects(Bucket=bucket, Delete={"Object": delete_list})
       print(response)
    if __name__ == "__main__":
       delete_cos_dir()
    
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support