All Products
Search
Document Center

Object Storage Service:Delete objects (Harmony SDK)

Last Updated:Mar 20, 2026

Use OSS SDK for HarmonyOS to delete a single object or multiple objects from a bucket.

Prerequisites

Before you begin, ensure that you have:

Delete a single object

Call deleteObject to remove one object from a bucket.

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

const client = new Client({
  accessKeyId: '<your-access-key-id>',        // AccessKey ID obtained from STS
  accessKeySecret: '<your-access-key-secret>', // AccessKey secret obtained from STS
  securityToken: '<your-sts-token>',           // STS token obtained from STS
  region: 'oss-cn-hangzhou',                   // Region where the bucket is located
});

const deleteObject = async () => {
  try {
    const res = await client.deleteObject({
      bucket: '<your-bucket-name>',
      key: '<your-object-key>',
    });
    console.log(JSON.stringify(res));
  } catch (err) {
    if (err instanceof RequestError) {
      console.log('code: ', err.code);
      console.log('message: ', err.message);
      console.log('requestId: ', err.requestId);
      console.log('status: ', err.status);
      console.log('ec: ', err.ec);
    } else {
      console.log('unknown error: ', err);
    }
  }
};

deleteObject();

Parameters

ParameterTypeRequiredDescription
bucketStringYesName of the bucket that contains the object
keyStringYesFull key (path) of the object to delete

Replace the following placeholders with actual values:

PlaceholderDescriptionExample
<your-access-key-id>AccessKey ID obtained from Security Token Service (STS)LTAI5tXxx
<your-access-key-secret>AccessKey secret obtained from STSxXxXxXx
<your-sts-token>STS token obtained from STSCAISxxx
<your-bucket-name>Name of the bucketmy-bucket
<your-object-key>Full key of the object to deleteimages/photo.jpg

Delete multiple objects

Call deleteMultipleObjects to remove multiple objects in a single request.

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

const client = new Client({
  accessKeyId: '<your-access-key-id>',
  accessKeySecret: '<your-access-key-secret>',
  securityToken: '<your-sts-token>',
  region: 'oss-cn-hangzhou',
});

const deleteMultipleObjects = async () => {
  try {
    const res = await client.deleteMultipleObjects({
      bucket: '<your-bucket-name>',
      delete: {
        object: [
          { key: '<your-object-key-1>' },
          { key: '<your-object-key-2>' },
          { key: '<your-object-key-3>' },
        ],
      },
    });
    console.log(JSON.stringify(res));
  } catch (err) {
    if (err instanceof RequestError) {
      console.log('code: ', err.code);
      console.log('message: ', err.message);
      console.log('requestId: ', err.requestId);
      console.log('status: ', err.status);
      console.log('ec: ', err.ec);
    } else {
      console.log('unknown error: ', err);
    }
  }
};

deleteMultipleObjects();

Parameters

ParameterTypeRequiredDescription
bucketStringYesName of the bucket that contains the objects
delete.objectArrayYesList of objects to delete. Each entry must include a key field

Replace the following placeholders with actual values:

PlaceholderDescriptionExample
<your-bucket-name>Name of the bucketmy-bucket
<your-object-key-1> ... <your-object-key-3>Full key of each object to deletelogs/2024-01-01.log

What's next