AWS S3
Node.js
folder deletion
cloud storage
programming tutorial

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.

Practice system design

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/credentials file 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:ListBucket and s3:DeleteObject permissions for the operation.
  • Multiple Requests: If the folder contains many objects (more than 1000), multiple requests are required since listObjectsV2 has 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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.