Delete files, directories and buckets in amazon s3 java
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Deleting data in Amazon S3 is simple at the API level, but the terminology can be misleading. S3 stores objects in buckets and does not have real directories in the filesystem sense. What looks like a directory is just a key prefix, and a bucket can be deleted only after every relevant object is removed. In Java, the AWS SDK makes these operations manageable once you model them correctly.
Delete a single object
If you know the exact bucket and key, deleting one object is straightforward with the AWS SDK for Java 2.x.
This removes the object identified by that exact key. If the object does not exist, S3 generally treats the delete as idempotent rather than throwing a "file not found" style error.
Delete what people call a directory
In S3, a "directory" is just a prefix such as reports/2025/. To delete it, you list the objects under that prefix and delete those objects.
This is the right mental model for deleting a pseudo-directory in S3: delete every object whose key starts with the prefix.
Delete a bucket
A bucket cannot be deleted until it is empty. That means you must first delete its objects, and if versioning is enabled, you must also delete its object versions and delete markers.
For a non-versioned empty bucket, the final step is:
If the bucket still contains objects or versions, S3 rejects the request.
Versioned buckets need extra care
This is where many deletion scripts fail. In a versioned bucket, deleting the current visible object is not enough. Old versions and delete markers still count toward bucket contents. If your goal is to remove the bucket entirely, you must enumerate and delete versions as well.
That is not a Java quirk; it is core S3 behavior. The bucket is not truly empty until all versions and markers are gone.
Batch deletion is usually better than one-by-one deletion
For prefixes with many objects, repeated single-object delete calls are slow and noisy. deleteObjects lets you send multiple keys in one request, which is usually the better default for cleanup tools.
You still need pagination on the listing side, though, because large prefixes may span many result pages.
Common Pitfalls
The biggest mistake is thinking S3 directories are real directories. They are only key prefixes, so deleting a "directory" means deleting objects whose keys begin with that prefix.
Another issue is trying to delete a bucket before emptying it. S3 does not allow that, and versioned buckets add another layer because old versions must be removed too.
Developers also forget pagination when deleting large prefixes. A bucket listing call may return only part of the objects, leaving cleanup incomplete.
Finally, make sure the IAM permissions match the operation. Object deletion, listing, and bucket deletion each require the appropriate S3 permissions.
Summary
- S3 deletes objects by bucket and key, not by filesystem path.
- A "directory" in S3 is a prefix, so you delete all objects under that prefix.
- Buckets can be deleted only after they are completely empty.
- Versioned buckets require deleting versions and delete markers as well.
- For large cleanups, combine paginated listing with batch delete requests.

