All Products
Search
Document Center

Object Storage Service:Delete object tags

Last Updated:Oct 17, 2023

You can delete unnecessary tags of an object based on your requirements. If versioning is enabled for a bucket that stores the object whose tags you want to delete, Object Storage Service (OSS) deletes the tags of the current version of the object by default. To delete the tags of a specified version of the object, you must specify the version ID of the object.

Delete the tags of an object

If versioning is not enabled for the bucket that stores the object whose tags you want to delete, you can delete the tags of the object based on your requirements. If versioning is enabled for the bucket that stores the object whose tags you want to delete, OSS deletes the tags of the current version of the object by default.

The following code provides an example on how to delete the tags of the exampleobject.txt object in the exampledir directory of the examplebucket 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',
  // Obtain access credentials from environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  // Specify the name of the bucket. Example: examplebucket. 
  bucket: 'examplebucket',
});

// Specify the full path of the object. Example: exampledir/exampleobject.txt. Do not include the bucket name in the full path. 
constobjectName='exampledir/exampleobject.txt'

// Delete the tags of the object. 
async function deleteObjectTagging(objectName) {
  try {
    const result = await client.deleteObjectTagging(objectName);
    console.log(result);
  } catch (e) {
    console.log(e);
  }
}

deleteObjectTagging(objectName)

Delete the tags of a specified version of an object

If versioning is enabled for the bucket that stores the object whose tags you want to delete, you can delete the tags of a specified version of the object by specifying the version ID of the object.

The following code provides an example on how to delete the tags of a specified version of the exampleobject.txt object in the exampledir directory of the examplebucket bucket:

Note For more information about how to obtain the version IDs of an object, see List objects.
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',
  // Obtain access credentials from environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  // Specify the name of the bucket. Example: examplebucket. 
  bucket: 'examplebucket',
});

// Specify the full path of the object. Example: exampledir/exampleobject.txt. Do not include the bucket name in the full path. 
constobjectName='exampledir/exampleobject.txt'
// Specify the version ID of the object. 
const versionId='CAEQIRiBgMDqvPqA3BciIDJhMjE4MWZkN2ViYTRmYzJhZjkxMzk2YWM2NjJk****'

// Delete the tags of the object. 
async function deleteObjectTagging(objectName) {
  try {
    const options = {
      versionId
    };
    const result = await client.deleteObjectTagging(objectName, options);
    console.log(result);
  } catch (e) {
    console.log(e);
  }
}

deleteObjectTagging(objectName)

References

  • For the complete sample code that is used to delete the tags of an object, visit GitHub.

  • For more information about the API operation that you can call to delete the tags of an object, see DeleteObjectTagging.