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 versioned bucket

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

  • Do not specify a version ID (temporary deletion)

    By default, if you do not specify a version ID in a DeleteObject request, OSS does not delete the current version of the object but add a delete marker to the object as the new current version. If you perform the GetObject operation on the object, OSS identifies that the current version of the object is a delete marker, and then returns 404 Not Found. In addition, 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 is the version ID of a delete marker.

  • Specify a version ID (permanent deletion)

    If you specify a version ID in a DeleteObject request, OSS permanently deletes the version whose ID is specified by the versionId field in the params parameter. To delete a version whose ID is null, add params['versionId'] = "null" in 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 describe 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 with a specified version ID from a versioned bucket:

    const OSS = require('ali-oss');
    
    const client = new OSS({
      bucket: '<Your BucketName>',
      // The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint.
      region: '<Your Region>',
      // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
      accessKeyId: '<Your AccessKeyId>',
      accessKeySecret: '<Your AccessKeySecret>'
    });
    
    // Delete the object with the specified version ID.
    const versionId = 'your versionId';
    
    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:

    const OSS = require('ali-oss');
    
    const client = new OSS({
      bucket: '<Your BucketName>',
      // The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint.
      region: '<Your Region>',
      // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
      accessKeyId: '<Your AccessKeyId>',
      accessKeySecret: '<Your AccessKeySecret>'
    });
    
    // 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();

    For more information about how to delete a single object, see DeleteObject.

Delete multiple objects

The following examples describe 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 with specified version IDs from a versioned bucket:

    const OSS = require('ali-oss');
    
    const client = new OSS({
      bucket: '<Your BucketName>',
      // The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint.
      region: '<Your Region>',
      // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
      accessKeyId: '<Your AccessKeyId>',
      accessKeySecret: '<Your AccessKeySecret>'
    });
    
    // 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' },
      { key: 'key2.js', versionId: 'versionId2' }
    ];
    
    async function deleteMulti() {
      const result = await client.deleteMulti(names);
      console.log(result);
    }
    deleteMulti();

    For more information about how to delete multiple objects, see DeleteMultipleObjects.

  • 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({
      bucket: '<Your BucketName>',
      // The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint.
      region: '<Your Region>',
      // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
      accessKeyId: '<Your AccessKeyId>',
      accessKeySecret: '<Your AccessKeySecret>'
    });
    
    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({
  bucket: '<Your BucketName>',
  // The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint.
  region: '<Your Region>',
  // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
  accessKeyId: '<Your AccessKeyId>',
  accessKeySecret: '<Your AccessKeySecret>'
});

async function deleteMutiPrefix(prefix) {
  // List the versions of the object 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
    });
  }
}