All Products
Search
Document Center

Object Storage Service:Manage directories (Node.js SDK)

Last Updated:Mar 20, 2026

OSS stores objects in a flat namespace with no native folder hierarchy. To organize large numbers of objects, OSS simulates a folder structure through the directory feature: a directory is an object whose key ends with a forward slash (/). All objects whose keys share that prefix appear to belong to the directory.

Prerequisites

Before you begin, make sure you have:

  • An OSS bucket

  • The ali-oss package installed (npm install ali-oss)

  • OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET set as environment variables

Create a directory

To create a directory, upload an empty object with a key that ends in /. The following example creates exampledir/ in the bucket.

The directory name must end with a forward slash (/).
const OSS = require('ali-oss');

const client = new OSS({
  // Replace yourregion with the region where your bucket is located,
  // for example, oss-cn-hangzhou for China (Hangzhou).
  region: 'yourregion',
  // Read credentials from environment variables.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  bucket: 'examplebucket',
});

async function createDirectory() {
  try {
    const result = await client.put('exampledir/', new Buffer(''));
    console.log(result);
  } catch (e) {
    console.log(e);
  }
}

createDirectory();

Delete a directory

Warning

Deleting a directory deletes all objects and subdirectories under it. Proceed with caution.

First, list all objects under the prefix. Then delete them in parallel. The following example deletes the log/ directory and everything in it.

Each deletion is handled individually so that a single failure does not interrupt the rest of the batch. Failed deletions are returned in the result for inspection.

const OSS = require('ali-oss');

const client = new OSS({
  // Replace yourregion with the region where your bucket is located,
  // for example, oss-cn-hangzhou for China (Hangzhou).
  region: 'yourregion',
  // Read credentials from environment variables.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  bucket: 'yourbucketname',
});

// Delete a single object and return the error (instead of throwing)
// so that Promise.all() continues even if one deletion fails.
async function deleteOne(name) {
  try {
    await client.delete(name);
  } catch (error) {
    error.failObjectName = name;
    return error;
  }
}

// List all objects under the prefix and delete them in parallel.
async function deleteDirectory(prefix) {
  const list = await client.list({ prefix });
  const objects = list.objects || [];
  const results = await Promise.all(objects.map((obj) => deleteOne(obj.name)));
  console.log(results);
}

deleteDirectory('log/');

What's next

  • For the PutObject API used to create a directory, see PutObject.

  • For the DeleteObject API used to delete objects, see DeleteObject.

  • For the complete sample code, see the GitHub example.