This topic describes how to delete a single object, multiple objects, or objects whose names contain a specified prefix from a versioned bucket.

Delete operations in a versioned bucket

When you delete an object from a versioned bucket, you must determine whether to specify a version ID in the request.

  • Delete an object without specifying a version ID (temporary deletion)

    By default, if you do not specify the version ID of the object that you want to delete in the request, Object Storage Service (OSS) does not delete the current version of the object but adds a delete marker to the object as the new current version. If you perform the GetObject operation on the object, OSS identifies the current version of the object as a delete marker and returns 404 Not Found. Additionally, the x-oss-delete-marker header and the x-oss-version-id header that indicates the version ID of the created delete marker are included in the response to the DeleteObject request.

    The value of the x-oss-delete-marker header in the response is true, which indicates that the x-oss-version-id value is the version ID of a delete marker.

  • Delete an object by specifying a version ID (permanent deletion)

    If you specify the version ID of the object that you want to delete in the request, OSS permanently deletes the version specified by the versionId field in the params parameter. To delete a version whose ID is null, add params['versionId'] = "null" to params in the request. OSS identifies the string "null" as the ID of the version to delete and deletes the version whose ID is null.

Delete a single object

The following examples demonstrate how to permanently or temporarily delete a single object from a versioned bucket.

  • Permanently delete an object from a versioned bucket

    The following code provides an example on how to permanently delete an object from a versioned bucket by specifying the version ID of the object in the request:

    const OSS = require('ali-oss');
    
    const client = new OSS({
      // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou. 
      region: 'yourregion',
      // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
      accessKeyId: 'yourAccessKeyId',
      accessKeySecret: 'yourAccessKeySecret',
      // Set yourbucketname to the name of your bucket. 
      bucket: 'yourbucketname'
    });
    
    // Delete the object with the specified version ID. 
    const versionId = 'yourversionId';
    
    async function deleteVersionObject() {
      const result = await client.delete(obj.name, {
        versionId
      });
    }
    deleteVersionObject();
  • Temporarily delete an object from a versioned bucket

    The following code provides an example on how to temporarily delete an object from a versioned bucket by sending a request in which no version ID is specified:

    const OSS = require('ali-oss');
    
    const client = new OSS({
      // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou. 
      region: 'yourregion',
      // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
      accessKeyId: 'yourAccessKeyId',
      accessKeySecret: 'yourAccessKeySecret',
      // Set yourbucketname to the name of your bucket. 
      bucket: 'yourbucketname'
    });
    
    // Temporarily delete an object without specifying its version ID. A delete marker is added to the object. 
    async function deleteObject() {
      const result = await client.delete(obj.name);
    }
    deleteObject();

Delete multiple objects

The following examples show how to permanently or temporarily delete multiple objects from a versioned bucket.

  • Permanently delete multiple objects from a versioned bucket

    The following code provides an example on how to permanently delete multiple objects or delete markers from a versioned bucket by specifying the version IDs of the objects or delete markers in the request:

    const OSS = require('ali-oss');
    
    const client = new OSS({
      // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou. 
      region: 'yourregion',
      // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
      accessKeyId: 'yourAccessKeyId',
      accessKeySecret: 'yourAccessKeySecret',
      // Set yourbucketname to the name of your bucket. 
      bucket: 'yourbucketname'
    });
    
    // Delete the objects with specified version IDs or the objects whose current versions are delete markers with specified version IDs. 
    const names = [
      { key: 'key1.js', versionId: 'versionId1' },
      null
    ];
    
    async function deleteMulti() {
      const result = await client.deleteMulti(names);
      console.log(result);
    }
    deleteMulti();
  • Temporarily delete multiple objects from a versioned bucket

    The following code provides an example on how to temporarily delete multiple objects from a versioned bucket:

    const OSS = require('ali-oss');
    
    const client = new OSS({
      // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou. 
      region: 'yourregion',
      // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
      accessKeyId: 'yourAccessKeyId',
      accessKeySecret: 'yourAccessKeySecret',
      // Set yourbucketname to the name of your bucket. 
      bucket: 'yourbucketname'
    });
    
    const names = ['key1.js', 'key2.js'];
    
    async function deleteMulti() {
      // Delete multiple objects without specifying their version IDs. 
      const result = await client.deleteMulti(names);
      console.log(result);
    }
    deleteMulti();

Delete objects whose names contain a specified prefix

The following code provides an example on how to delete objects whose names contain a specified prefix:

const OSS = require('ali-oss');

const client = new OSS({
  // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou. 
  region: 'yourregion',
  // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
  accessKeyId: 'yourAccessKeyId',
  accessKeySecret: 'yourAccessKeySecret',
  // Set yourbucketname to the name of your bucket. 
  bucket: 'yourbucketname'
});

async function deleteMutiPrefix(prefix) {
  // List the versions of the objects whose names contain the specified prefix. 
  const list = client.getBucketVersions({
    prefix: prefix,
  });

  list.objects = list.objects || [];
  for (let i = 0; i < result.objects.length; i++) {
    const obj = result.objects[i]; 
    // Delete the objects whose names contain the specified prefix. 
    const versionId = obj.versionId; 
    await client.delete(obj.name, {
      versionId
    });
  }
}

References

  • For more information about the API operation that you can call to delete a single object, see DeleteObject.
  • For more information about the API operation that you can call to delete multiple objects, see DeleteMultipleObjects.