AWS S3
JavaScript
Object Deletion
Cloud Storage
Programming Tutorial

How do I delete an object on AWS S3 using JavaScript?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Deleting an object from S3 in JavaScript is straightforward once you know the bucket name, object key, and have credentials with s3:DeleteObject permission. The main thing to remember is that "deleting a file" in S3 really means deleting an object identified by its exact key.

Using the AWS SDK for JavaScript v3

For modern code, use the AWS SDK v3 client packages.

bash
npm install @aws-sdk/client-s3

Then create an S3 client and send a DeleteObjectCommand:

javascript
1import { S3Client, DeleteObjectCommand } from "@aws-sdk/client-s3";
2
3const client = new S3Client({ region: "us-east-1" });
4
5async function deleteObject(bucket, key) {
6  const command = new DeleteObjectCommand({
7    Bucket: bucket,
8    Key: key,
9  });
10
11  await client.send(command);
12  console.log(`Deleted ${key} from ${bucket}`);
13}
14
15await deleteObject("my-bucket", "images/avatar.png");

The Key must match the full object path inside the bucket. In S3, folders are just prefixes in the key name.

Handling errors

In production code, wrap the request in try and catch so you can handle permission problems, wrong regions, or missing credentials.

javascript
1import { S3Client, DeleteObjectCommand } from "@aws-sdk/client-s3";
2
3const client = new S3Client({ region: "us-east-1" });
4
5async function safeDelete(bucket, key) {
6  try {
7    await client.send(new DeleteObjectCommand({ Bucket: bucket, Key: key }));
8    return { ok: true };
9  } catch (error) {
10    console.error("Delete failed:", error);
11    return { ok: false, error };
12  }
13}

If the object does not exist, S3 delete behavior is generally idempotent for ordinary cases. That means the request can still succeed even when the key is already absent.

Deleting versioned objects

If bucket versioning is enabled, deleting without a VersionId usually creates a delete marker instead of erasing a specific historical version. To delete a particular version, include VersionId:

javascript
1await client.send(
2  new DeleteObjectCommand({
3    Bucket: "my-bucket",
4    Key: "images/avatar.png",
5    VersionId: "3LgkqExampleVersionId",
6  })
7);

This distinction matters a lot when you are debugging "the object is still there" in a versioned bucket.

Deleting multiple objects

If you need to delete many keys at once, use the bulk delete API rather than sending single-object requests in a loop.

javascript
1import { DeleteObjectsCommand, S3Client } from "@aws-sdk/client-s3";
2
3const client = new S3Client({ region: "us-east-1" });
4
5await client.send(
6  new DeleteObjectsCommand({
7    Bucket: "my-bucket",
8    Delete: {
9      Objects: [
10        { Key: "tmp/a.txt" },
11        { Key: "tmp/b.txt" },
12      ],
13    },
14  })
15);

That is more efficient for cleanup jobs and batch operations.

Credentials and environment

The SDK can load credentials from several places, including environment variables, shared AWS config files, IAM roles, and container task roles. For local development, a common setup is:

bash
export AWS_ACCESS_KEY_ID=...
export AWS_SECRET_ACCESS_KEY=...
export AWS_REGION=us-east-1

In deployed environments, prefer IAM roles over hard-coded credentials.

Common Pitfalls

  • Passing the wrong Key, especially when the object is under a prefix such as uploads/2025/file.txt.
  • Forgetting that versioned buckets may create delete markers instead of removing historical versions.
  • Using credentials without s3:DeleteObject permission.
  • Assuming S3 has real folders and deleting only the visible prefix rather than the full object key.
  • Deleting objects one by one in batch jobs instead of using the bulk delete API.

Summary

  • Use DeleteObjectCommand from the AWS SDK for JavaScript v3 to delete a single S3 object.
  • The bucket name and exact object key are required.
  • Wrap deletes in error handling for permission and configuration issues.
  • In versioned buckets, VersionId changes what "delete" means.
  • For many objects, prefer DeleteObjectsCommand instead of repeated single deletes.

Course illustration
Course illustration

All Rights Reserved.