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.
Then create an S3 client and send a DeleteObjectCommand:
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.
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:
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.
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:
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 asuploads/2025/file.txt. - Forgetting that versioned buckets may create delete markers instead of removing historical versions.
- Using credentials without
s3:DeleteObjectpermission. - 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
DeleteObjectCommandfrom 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,
VersionIdchanges what "delete" means. - For many objects, prefer
DeleteObjectsCommandinstead of repeated single deletes.

