How do I delete an object on AWS S3 using JavaScript?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
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.
Related reading
- How do I disconnect from my EC2 Instance?
- How do I enable Encryption at Rest on DynamoDB with CloudFormation
- How do I expose Kubernetes service to the internet?
- How do I fail a specific SQS message in a batch from a Lambda?
- How do I detect whether a variable is a function?
- How do I detect whether a variable is a function?
- How do I find the total size of my AWS S3 storage bucket or folder?
- How do I get data storage used on my AWS RDS?

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.