This topic describes how to delete a single or multiple objects and objects whose names contain a specified prefix from a versioning-enabled bucket.

Delete a single object

If you do not specify the version ID when you perform the DeleteObject operation on an object in a versioning-enabled bucket, the current version of the object is retained. A delete marker is added and becomes the current version of the object. If you specify the version ID, the specified version of the object is permanently deleted.

Note
  • If you do not specify the version ID when you perform the DeleteObject operation:

    By default, the DeleteObject operation is performed on the current version of the target object. The current version is retained and a delete marker is inserted as the new current version of the object. When the GetObject operation is performed on the object, and OSS identifies that the current version of the object is a delete marker. OSS returns 404 Not Found. Additionally, the x-oss-delete-marker = true and x-oss-version-id headers are included in the response. x-oss-version-id indicates the version ID of the delete marker.

    If the value of the x-oss-delete-marker field is true, the version specified in the x-oss-version-id field is a delete marker.

  • If you specify the version ID when you perform the DeleteObject operation:

    OSS permanently deletes the object by specifying the version ID. For example, if you want to delete an object with a version ID of null, you must set the value of the params['versionId'] parameter in your request to null. OSS identifies the string null as a version ID and deletes the object whose version ID is null.

Examples of deleting a specified object version permanently and deleting an object temporarily are provided as follows:

  • Permanently delete an object with a specified version ID

    The following code provides an example on how to permanently delete an object by specifying a version ID:

    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 an 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 without specifying the version ID

    The following code provides an example on how to temporarily delete an object without specifying the version ID:

    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>'
    });
    
    // If you do not specify a version ID when you delete an object in a versioning-enabled bucket, the object is retained as a previous version and a delete marker is added to the object.
    async function deleteObject() {
      const result = await client.delete(obj.name);
    }
    deleteObject();

    For more information about deleting a single object, see DeleteObject.

Delete multiple objects

You can perform the DeleteObjects operation to delete multiple objects from a versioning-enabled bucket. If you do not specify the version IDs in your request, delete markers are added. If you specify the version IDs in your request, objects with the specified version IDs are permanently deleted.

  • Delete multiple objects without specifying version IDs

    The following code provides an example on how to delete multiple objects without specifying the version ID:

    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 the version IDs.
      const result = await client.deleteMulti(names);
      console.log(result);
    }
    deleteMulti();
  • Delete multiple objects with specified version IDs

    The following code provides an example on how to delete multiple objects by specifying the version IDs or delete markers by specifying the version IDs:

    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 objects or delete markers with the 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 deleting multiple objects, see DeleteMultipleObjects.

Delete objects with a specified prefix

The following code provides an example on how to delete an object with 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 version IDs of all objects with 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 objects with the specified prefix and version IDs.
    const versionId = obj.versionId; 
    await client.delete(obj.name, {
      versionId
    });
  }
}