You can delete a single object, multiple specified objects, objects whose names contain a specified prefix, or a specified directory and all objects in the directory.

Warning Deleted objects cannot be recovered. Exercise caution when you delete objects.

Delete a single object

The following code provides an example on how to delete an object named exampleobject.txt from a bucket named examplebucket:

const OSS = require('ali-oss')

const client = new OSS({
  // Specify the region in which the bucket is located. For example, if your 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 Object Storage Service (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',
  // Specify the bucket name. 
  bucket: 'examplebucket',
});

async function deleteFile () {
  try {
    // Specify the full path of the object. The full path of the object cannot contain the bucket name. 
    let result = await client.delete('exampleobject.txt');
    console.log(result);
  } catch (e) {
    console.log(e);
  }
}

deleteFile();            

Delete multiple objects

You can delete up to 1,000 objects at a time. You can delete multiple specified objects, objects whose names contain a specified prefix, or a specified directory and all objects in the directory.

When you delete multiple objects, the result can be returned in one of the following modes:
  • verbose: The response includes a message body that contains the information about all deleted objects. By default, this mode is used.
  • quiet: The response does not include a message body.

You can also configure lifecycle rules to automatically delete objects. For more information, see Lifecycle rules based on the last modified time.

Delete multiple specified objects

The following code provides an example on how to delete multiple specified objects:

const OSS = require('ali-oss')

const client = new OSS({
  // Specify the region in which the bucket is located. For example, if your 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',
  // Specify the bucket name. 
  bucket: 'examplebucket',
});

async function deleteMulti () {
  try {
    // Specify the full paths of the objects to delete and set the return mode to verbose. The full paths of the objects cannot contain the bucket name. 
    //let result = await client.deleteMulti(['exampleobject-1', 'exampleobject-2', 'testfolder/sampleobject.txt']);
    //console.log(result);
    // Specify the full paths of the objects to delete and set the return mode to quiet. The full paths of the objects cannot contain the bucket name. 
    let result = await client.deleteMulti(['exampleobject-1', 'exampleobject-2', 'testfolder/sampleobject.txt'], {quiet: true});
    console.log(result);
  } catch (e) {
    console.log(e);
  }
}

deleteMulti();            

Delete multiple objects with a specified prefix or in a specified directory

The following code provides an example on how to delete multiple objects with a specified prefix and how to delete a specified directory and all objects in the directory.

Warning If the prefix is not specified or is set to NULL in the following code, all objects in the bucket are deleted. Exercise caution when you configure the prefix.
const OSS = require('ali-oss');

const client = new OSS({
  // Specify the region in which the bucket is located. For example, if your 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',
  // Specify the bucket name. 
  bucket: 'examplebucket',
});

// Handle request failures, prevent the interruption of promise.all, and return failure causes and the names of objects that fail to be deleted. 
async function handleDel(name, options) {
  try {
    await client.delete(name);
  } catch (error) {
    error.failObjectName = name;
    return error;
  }
}

// Delete multiple objects. 
async function deletePrefix(prefix) {
  const list = await client.list({
    prefix: prefix,
  });

  list.objects = list.objects  [];
  const result = await Promise.all(list.objects.map((v) => handleDel(v.name)));
  console.log(result);
}
// Specify the prefix of the objects that you want to delete. If you want to delete all objects whose names contain the src prefix, set the prefix to src. After you set the prefix to src, all non-directory objects whose names contain the src prefix, the src directory, and all objects in the directory are deleted. 
deletePrefix('src')
// If you want to delete only the src directory and all objects in the directory, set the prefix to src/. 
// deletePrefix('src/')

References

  • For the complete sample code that is used to delete a single object or multiple objects, visit GitHub.
  • 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.