All Products
Search
Document Center

Object Storage Service:Delete object using Harmony SDK

Last Updated:Mar 11, 2025

This topic explains how to delete a single object or multiple objects using the OSS Harmony SDK.

Precautions

Sample code

Delete a single object

import Client, { RequestError } from '@aliyun/oss';

// Create an OSS client instance
const client = new Client({
  // Replace with the Access Key ID of the STS temporary access credential
  accessKeyId: 'yourAccessKeyId',
  // Replace with the Access Key Secret of the STS temporary access credential
  accessKeySecret: 'yourAccessKeySecret',
  // Replace with the Security Token of the STS temporary access credential
  securityToken: 'yourSecurityToken',
  // Specify the region of the bucket. For example, if the region is China (Hangzhou), set the region to oss-cn-hangzhou
  region: 'oss-cn-hangzhou',
});

// Specify the name of the bucket to operate on. Replace with the name of your actual bucket
const bucket = 'yourBucketName';
// Specify the name of the object (file) to delete. Replace with the name of the actual object you want to delete
const key = 'yourObjectName';

/**
 * Delete the specified object.
 * Use the deleteObject method to delete an object in the specified bucket.
 */
const deleteObject = async () => {
  try {
    // Call the deleteObject method to delete an object in the specified bucket
    const res = await client.deleteObject({
      bucket, // Bucket name
      key,    // Object (file) name
    });

    // Print the result of the deletion
    console.log(JSON.stringify(res));
  } catch (err) {
    // Catch exception information during the request
    if (err instanceof RequestError) {
      // If it is a known type of error, print the error code, error message, request ID, status code, EC code, and other information
      console.log('code: ', err.code); // Error code
      console.log('message: ', err.message); // Error message
      console.log('requestId: ', err.requestId); // Request ID
      console.log('status: ', err.status); // HTTP status code
      console.log('ec: ', err.ec); // Error code
    } else {
      // Print other unknown types of errors
      console.log('unknown error: ', err);
    }
  }
};

// Call the deleteObject function to perform the delete object operation
deleteObject();

Delete multiple objects

import Client, { RequestError } from '@aliyun/oss';

// Create an OSS client instance
const client = new Client({
  // Replace with the Access Key ID of the STS temporary access credential
  accessKeyId: 'yourAccessKeyId',
  // Replace with the Access Key Secret of the STS temporary access credential
  accessKeySecret: 'yourAccessKeySecret',
  // Replace with the Security Token of the STS temporary access credential
  securityToken: 'yourSecurityToken',
  // Specify the region of the bucket. For example, if the region is China (Hangzhou), set the region to oss-cn-hangzhou
  region: 'oss-cn-hangzhou',
});

// Specify the name of the bucket to operate on. Replace with the name of your actual bucket
const bucket = 'yourBucketName';

/**
 * Batch delete objects.
 * Use the deleteMultipleObjects method to batch delete multiple objects in the specified bucket.
 */
const deleteMultipleObjects = async () => {
  try {
    // Call the deleteMultipleObjects method to batch delete multiple objects in the specified bucket
    const res = await client.deleteMultipleObjects({
      bucket, // Bucket name
      delete: {
        // Specify the list of objects to delete, each object is identified by its key
        object: [
          { key: 'yourObjectName1' }, // Name of object 1. Replace with the name of the actual object you want to delete
          { key: 'yourObjectName2' }, // Name of object 2. Replace with the name of the actual object you want to delete
          { key: 'yourObjectName3' }, // Name of object 3. Replace with the name of the actual object you want to delete
        ],
      },
    });

    // Print the result of the batch deletion
    console.log(JSON.stringify(res));
  } catch (err) {
    // Catch exception information during the request
    if (err instanceof RequestError) {
      // If it is a known type of error, print the error code, error message, request ID, status code, EC code, and other information
      console.log('code: ', err.code); // Error code
      console.log('message: ', err.message); // Error message
      console.log('requestId: ', err.requestId); // Request ID
      console.log('status: ', err.status); // HTTP status code
      console.log('ec: ', err.ec); // Error code
    } else {
      // Print other unknown types of errors
      console.log('unknown error: ', err);
    }
  }
};

// Call the deleteMultipleObjects function to perform the batch delete objects operation
deleteMultipleObjects();