You can use brute force:
List<String> matchingKeys = new ArrayList<>();
Predicate<String> predicate = Pattern.compile(".*/" + filename + "\\.\\w+").asPredicate();
for (ObjectListing listing = s3.listObjects(bucketName, "/");; listing = s3.listNextBatchOfObjects(listing)) {
listing.getObjectSummaries().stream()
.map(S3ObjectSummary::getKey)
.filter(predicate)
.forEach(matchingKeys::add);
if (!listing.isTruncated()) {
break;
}
}
Instead of collecting the hits, you could just delete:
.forEach(key -> s3.deleteObject(bucketName, key));
Note: This uses a regex suitable for your scenario of finding a key given the extension-less filename, but you could adjust the regex as you need. For example, to find all keys that have a substring anywhere in their key, use the regex
".*" + substring ".*"
Or case insensitive
"(?i).*" + substring ".*"
etc.
Disclaimer: Code may not compile or work as it was thumbed in on my phone (but there's a reasonable chance it will work)