How can I delete folder on S3 with Node.js?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Amazon S3 (Simple Storage Service) is one of the most popular storage services offered by AWS, providing scalable storage solutions in the cloud. Operating with objects and directories (conceptually) can sometimes be tricky because S3 doesn’t have a built-in hierarchical file system like a traditional file system. However, you can manage these operations effectively using the AWS SDK for Node.js. In this guide, we'll focus on how you can programmatically delete a folder in Amazon S3 using Node.js.
Understanding Amazon S3 Folders
In Amazon S3, what we conceive as folders are technically part of an object's key (the unique identifier for an object). For example, in the key folder1/folder2/file.txt
, folder1/folder2/
acts like a directory path, but technically, it is part of the object's key. To "delete a folder," you need to remove all objects (files) that have keys prefixed with the folder's path.
Prerequisites
Before we dive into the technical implementations, ensure you have the following:
- An AWS account.
- Node.js installed on your machine.
- The AWS SDK for Node.js (usually the latest version).
- Proper AWS credentials configured on your system (usually via the
~/.aws/credentialsfile or AWS environment variables).
Setting Up AWS SDK for Node.js
First, you need to install the SDK. You can do this via npm:
- IAM Permissions: Ensure the credentials you use have
s3:ListBucketands3:DeleteObjectpermissions for the operation. - Multiple Requests: If the folder contains many objects (more than 1000), multiple requests are required since
listObjectsV2has a maximum limit of 1000 objects per request. - Error Handling: Always include proper error handling to manage any unexpected issues or rate limits enforced by AWS.
- Asynchronous by Nature: Node.js's event-driven environment is suited for I/O operations, making it a good fit for network operations like S3 interactions.
- Single Language Stack: Developers working on JavaScript-based applications can maintain a single language stack on both client and server.
- Wide AWS Service Support: AWS SDK provides various features and services that integrate smoothly with other AWS services beyond S3.
Related reading
- How can I determine the current ephemeral-storage usage of a running Kubernetes pod?
- How can I distribute a deployment across nodes?
- How can I easily determine if a Boto 3 S3 bucket resource exists?
- How can I fetch all items from a DynamoDB table without specifying the primary key with java?
- How can I detect pressing Enter on the keyboard using jQuery?
- How can I determine equality for two JavaScript objects?
- How can I find out why my storage space on Amazon EC2 is full?
- How can I find the arn of an api gateway stage?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.