How to delete a directory from spaces

From your example ${BUCKET}.${REGION}${OBJECT_KEY}

I’m storing directories that are a result of a ten-digit galleryID example: ${BUCKET}.${REGION}}

I thought this would work but it does not. s3.delete(${BUCKET}.${REGION}${galleryID}, (error, data)=> {})

How can I send a delete request using the example above?

I am using nodeJS with KoaJS


Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

I found the answer on StackOverflow for anyone interested.

async function emptyS3Directory(bucket, dir) {
    const listParams = {
        Bucket: bucket,
        Prefix: dir

    const listedObjects = await s3.listObjectsV2(listParams).promise();

    if (listedObjects.Contents.length === 0) return;

    const deleteParams = {
        Bucket: bucket,
        Delete: { Objects: [] }

    listedObjects.Contents.forEach(({ Key }) => {
        deleteParams.Delete.Objects.push({ Key });

    await s3.deleteObjects(deleteParams).promise();

    if (listedObjects.IsTruncated) await emptyS3Directory(bucket, dir);

Call it

await emptyS3Directory(process.env.S3_BUCKET, 'images/')

Hello, @mrtonyq22

You can easily delete the directory from your DigitalOcean account as well.

Just navigate to the Space in your account and then click the “More” button and select Delete

I personally use s3cmd to manage my Spaces due to the fact it is really user friendly. You can check this article in order to install s3cmd

and check this mini tutorial on how to operate with s3cmd

An example command with s3cmd to delete a directory:

  1. s3cmd del s3://newspace/test

Hope that this helps Regards, Alex