• Object Storage Service

obs
  1. Help Center
  2. Object Storage Service
  3. Developer Guide (Python SDK)
  4. Object Management
  5. Deleting Objects

Deleting Objects

Deleting a Single Object

You can call ObsClient.deleteObject to delete a single object. Sample code is as follows:

# Import the module.
from com.obs.client.obs_client import ObsClient
 
# Create an instance of ObsClient.
obsClient = ObsClient(
    access_key_id='*** Provide your Access Key ***',    
    secret_access_key='*** Provide your Secret Key ***',    
    server='yourdomainname'
)

resp = obsClient.deleteObject('bucketname', 'objectkey')
if resp.status < 300:
    print('requestId:', resp.requestId)
else:
    print('errorCode:', resp.errorCode)
    print('errorMessage:', resp.errorMessage)

Batch Deleting Objects

You can call ObsClient.deleteObjects to delete objects in a batch.

A maximum of 1000 objects can be deleted each time. Two response modes are supported: verbose (detailed) and quiet (brief).

  • In verbose mode (default mode), the returned response includes the deletion result of each requested object.
  • In quiet mode, the returned response includes only results of objects failed to be deleted.

Sample code:

# Import the module.
from com.obs.client.obs_client import ObsClient
 
# Create an instance of ObsClient.
obsClient = ObsClient(
    access_key_id='*** Provide your Access Key ***',    
    secret_access_key='*** Provide your Secret Key ***',    
    server='yourdomainname'
)
from com.obs.models.delete_objects_request import DeleteObjectsRequest, Object
deleteObjectsRequest = DeleteObjectsRequest()
# Set the response mode to verbose.
deleteObjectsRequest.quiet = False
deleteObjectsRequest.objects = [Object(key='objectkey1', versionId=None), Object(key='objectkey2', versionId=None)]
deleteObjectsRequest.objects.append(Object(key='objectkey3', versionId=None))

resp = obsClient.deleteObjects('bucketname', deleteObjectsRequest=deleteObjectsRequest)
if resp.status < 300:
    print('requestId:', resp.requestId)
    # Obtain the successfully deleted objects.
    if resp.body.deleted:
        index = 1
        for delete in resp.body.deleted:
            print('deleted[', index, ']:')
            print('key:', delete.key)
            print('versionId:', delete.versionId)
            index += 1
    # Obtain the list of objects failed to be deleted.
    if resp.body.error:
        index = 1
        for err in resp.body.error:
            print('error[', index, ']:')
            print('key:', err.key)
            print('code:', err.code)
            print('message:', err.message)
else:
    print('errorCode:', resp.errorCode)
    print('errorMessage:', resp.errorMessage)