The name of the bucket from which the
/// contents will be deleted.
///
DeleteBucketContentsAsync(IAmazonS3 client, string bucketName)
// Iterate over the contents of the bucket and delete all objects.
var request = new ListObjectsV2Request
BucketName = bucketName,
ListObjectsV2Response response;
response = await client.ListObjectsV2Async(request);
response.S3Objects
.ForEach(async obj => await client.DeleteObjectAsync(bucketName, obj.Key));
// If the response is truncated, set the request ContinuationToken
// from the NextContinuationToken property of the response.
request.ContinuationToken = response.NextContinuationToken;
while (response.IsTruncated);
return true;
catch (AmazonS3Exception ex)
Console.WriteLine($"Error deleting objects:
{
ex.Message}");
return false;
/// <summary>
/// This example shows how to delete multiple objects from an Amazon Simple
/// Storage Service (Amazon S3) bucket.
/// </summary>
public class DeleteMultipleObjects
/// <summary>
/// The Main method initializes the Amazon S3 client and the name of
/// the bucket and then passes those values to MultiObjectDeleteAsync.
/// </summary>
public static async Task Main()
const string bucketName = "amzn-s3-demo-bucket";
// If the Amazon S3 bucket from which you wish to delete objects is not
// located in the same AWS Region as the default user, define the
// AWS Region for the Amazon S3 bucket as a parameter to the client
// constructor.
IAmazonS3 s3Client = new AmazonS3Client();
await MultiObjectDeleteAsync(s3Client, bucketName);
/// <summary>
/// This method uses the passed Amazon S3 client to first create and then
/// delete three files from the named bucket.
/// </summary>
/// <param name="client">The initialized Amazon S3 client object used to call
/// Amazon S3 methods.</param>
/// <param name="bucketName">The name of the Amazon S3 bucket where objects
/// will be created and then deleted.</param>
public static async Task MultiObjectDeleteAsync(IAmazonS3 client, string bucketName)
// Create three sample objects which we will then delete.
var keysAndVersions = await PutObjectsAsync(client, 3, bucketName);
// Now perform the multi-object delete, passing the key names and
// version IDs. Since we are working with a non-versioned bucket,
// the object keys collection includes null version IDs.
DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest
BucketName = bucketName,
Objects = keysAndVersions,
// You can add a specific object key to the delete request using the
// AddKey method of the multiObjectDeleteRequest.
DeleteObjectsResponse response = await client.DeleteObjectsAsync(multiObjectDeleteRequest);
Console.WriteLine("Successfully deleted all the
{
0} items", response.DeletedObjects.Count);
catch (DeleteObjectsException e)
PrintDeletionErrorStatus(e);
/// <summary>
/// Prints the list of errors raised by the call to DeleteObjectsAsync.
/// </summary>
/// <param name="ex">A collection of exceptions returned by the call to
/// DeleteObjectsAsync.</param>
public static void PrintDeletionErrorStatus(DeleteObjectsException ex)
DeleteObjectsResponse errorResponse = ex.Response;
Console.WriteLine("x
{
0}", errorResponse.DeletedObjects.Count);
Console.WriteLine($"Successfully deleted
{
errorResponse.DeletedObjects.Count}.");
Console.WriteLine($"No. of objects failed to delete =
{
errorResponse.DeleteErrors.Count}");
Console.WriteLine("Printing error data...");
foreach (DeleteError deleteError in errorResponse.DeleteErrors)
Console.WriteLine($"Object Key:
{
deleteError.Key}\t
{
deleteError.Code}\t
{
deleteError.Message}");
/// <summary>
/// This method creates simple text file objects that can be used in
/// the delete method.
/// </summary>
/// <param name="client">The Amazon S3 client used to call PutObjectAsync.</param>
/// <param name="number">The number of objects to create.</param>
/// <param name="bucketName">The name of the bucket where the objects
/// will be created.</param>
/// <returns>A list of keys (object keys) and versions that the calling
/// method will use to delete the newly created files.</returns>
public static async Task<List<KeyVersion>> PutObjectsAsync(IAmazonS3 client, int number, string bucketName)
List<KeyVersion> keys = new List<KeyVersion>();
for (int i = 0; i < number; i++)
string key = "ExampleObject-" + new System.Random().Next();
PutObjectRequest request = new PutObjectRequest
BucketName = bucketName,
Key = key,
ContentBody = "This is the content body!",
PutObjectResponse response = await client.PutObjectAsync(request);
// For non-versioned bucket operations, we only need the
// object key.
KeyVersion keyVersion = new KeyVersion
Key = key,
keys.Add(keyVersion);
return keys;
/// <summary>
/// This example shows how to delete objects in a version-enabled Amazon
/// Simple StorageService (Amazon S3) bucket.
/// </summary>
public class DeleteMultipleObjects
public static async Task Main()
string bucketName = "amzn-s3-demo-bucket";
// If the AWS Region for your Amazon S3 bucket is different from
// the AWS Region of the default user, define the AWS Region for
// the Amazon S3 bucket and pass it to the client constructor
// like this:
// RegionEndpoint bucketRegion = RegionEndpoint.USWest2;
IAmazonS3 s3Client;
s3Client = new AmazonS3Client();
await DeleteMultipleObjectsFromVersionedBucketAsync(s3Client, bucketName);
/// <summary>
/// This method removes multiple versions and objects from a
/// version-enabled Amazon S3 bucket.
/// </summary>
/// <param name="client">The initialized Amazon S3 client object used to call
/// DeleteObjectVersionsAsync, DeleteObjectsAsync, and
/// RemoveDeleteMarkersAsync.</param>
/// <param name="bucketName">The name of the bucket from which to delete
/// objects.</param>
public static async Task DeleteMultipleObjectsFromVersionedBucketAsync(IAmazonS3 client, string bucketName)
// Delete objects (specifying object version in the request).
await DeleteObjectVersionsAsync(client, bucketName);
// Delete objects (without specifying object version in the request).
var deletedObjects = await DeleteObjectsAsync(client, bucketName);
// Additional exercise - remove the delete markers Amazon S3 returned from
// the preceding response. This results in the objects reappearing
// in the bucket (you can verify the appearance/disappearance of
// objects in the console).
await RemoveDeleteMarkersAsync(client, bucketName, deletedObjects);
/// <summary>
/// Creates and then deletes non-versioned Amazon S3 objects and then deletes
/// them again. The method returns a list of the Amazon S3 objects deleted.
/// </summary>
/// <param name="client">The initialized Amazon S3 client object used to call
/// PubObjectsAsync and NonVersionedDeleteAsync.</param>
/// <param name="bucketName">The name of the bucket where the objects
/// will be created and then deleted.</param>
/// <returns>A list of DeletedObjects.</returns>
public static async Task<List<DeletedObject>> DeleteObjectsAsync(IAmazonS3 client, string bucketName)
// Upload the sample objects.
var keysAndVersions2 = await PutObjectsAsync(client, bucketName, 3);
// Delete objects using only keys. Amazon S3 creates a delete marker and
// returns its version ID in the response.
List<DeletedObject> deletedObjects = await NonVersionedDeleteAsync(client, bucketName, keysAndVersions2);
return deletedObjects;
/// <summary>
/// This method creates several temporary objects and then deletes them.
/// </summary>
/// <param name="client">The S3 client.</param>
/// <param name="bucketName">Name of the bucket.</param>
/// <returns>Async task.</returns>
public static async Task DeleteObjectVersionsAsync(IAmazonS3 client, string bucketName)
// Upload the sample objects.
var keysAndVersions1 = await PutObjectsAsync(client, bucketName, 3);
// Delete the specific object versions.
await VersionedDeleteAsync(client, bucketName, keysAndVersions1);
/// <summary>
/// Displays the list of information about deleted files to the console.
/// </summary>
/// <param name="e">Error information from the delete process.</param>
private static void DisplayDeletionErrors(DeleteObjectsException e)
var errorResponse = e.Response;
Console.WriteLine($"No. of objects successfully deleted =
{
errorResponse.DeletedObjects.Count}");
Console.WriteLine($"No. of objects failed to delete =
{
errorResponse.DeleteErrors.Count}");
Console.WriteLine("Printing error data...");
foreach (var deleteError in errorResponse.DeleteErrors)
Console.WriteLine($"Object Key:
{
deleteError.Key}\t
{
deleteError.Code}\t
{
deleteError.Message}");
/// <summary>
/// Delete multiple objects from a version-enabled bucket.
/// </summary>
/// <param name="client">The initialized Amazon S3 client object used to call
/// DeleteObjectVersionsAsync, DeleteObjectsAsync, and
/// RemoveDeleteMarkersAsync.</param>
/// <param name="bucketName">The name of the bucket from which to delete
/// objects.</param>
/// <param name="keys">A list of key names for the objects to delete.</param>
private static async Task VersionedDeleteAsync(IAmazonS3 client, string bucketName, List<KeyVersion> keys)
var multiObjectDeleteRequest = new DeleteObjectsRequest
BucketName = bucketName,
Objects = keys, // This includes the object keys and specific version IDs.
Console.WriteLine("Executing VersionedDelete...");
DeleteObjectsResponse response = await client.DeleteObjectsAsync(multiObjectDeleteRequest);
Console.WriteLine($"Successfully deleted all the
{
response.DeletedObjects.Count} items");
catch (DeleteObjectsException ex)
DisplayDeletionErrors(ex);
/// <summary>
/// Deletes multiple objects from a non-versioned Amazon S3 bucket.
/// </summary>
/// <param name="client">The initialized Amazon S3 client object used to call
/// DeleteObjectVersionsAsync, DeleteObjectsAsync, and
/// RemoveDeleteMarkersAsync.</param>
/// <param name="bucketName">The name of the bucket from which to delete
/// objects.</param>
/// <param name="keys">A list of key names for the objects to delete.</param>
/// <returns>A list of the deleted objects.</returns>
private static async Task<List<DeletedObject>> NonVersionedDeleteAsync(IAmazonS3 client, string bucketName, List<KeyVersion> keys)
// Create a request that includes only the object key names.
DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest();
multiObjectDeleteRequest.BucketName = bucketName;
foreach (var key in keys)
multiObjectDeleteRequest.AddKey(key.Key);
// Execute DeleteObjectsAsync.
// The DeleteObjectsAsync method adds a delete marker for each
// object deleted. You can verify that the objects were removed
// using the Amazon S3 console.
DeleteObjectsResponse response;
Console.WriteLine("Executing NonVersionedDelete...");
response = await client.DeleteObjectsAsync(multiObjectDeleteRequest);
Console.WriteLine("Successfully deleted all the
{
0} items", response.DeletedObjects.Count);
catch (DeleteObjectsException ex)
DisplayDeletionErrors(ex);
throw; // Some deletions failed. Investigate before continuing.
// This response contains the DeletedObjects list which we use to delete the delete markers.
return response.DeletedObjects;
/// <summary>
/// Deletes the markers left after deleting the temporary objects.
/// </summary>
/// <param name="client">The initialized Amazon S3 client object used to call
/// DeleteObjectVersionsAsync, DeleteObjectsAsync, and
/// RemoveDeleteMarkersAsync.</param>
/// <param name="bucketName">The name of the bucket from which to delete
/// objects.</param>
/// <param name="deletedObjects">A list of the objects that were deleted.</param>
private static async Task RemoveDeleteMarkersAsync(IAmazonS3 client, string bucketName, List<DeletedObject> deletedObjects)
var keyVersionList = new List<KeyVersion>();
foreach (var deletedObject in deletedObjects)
KeyVersion keyVersion = new KeyVersion
Key = deletedObject.Key,
VersionId = deletedObject.DeleteMarkerVersionId,
keyVersionList.Add(keyVersion);
// Create another request to delete the delete markers.
var multiObjectDeleteRequest = new DeleteObjectsRequest
BucketName = bucketName,
Objects = keyVersionList,
// Now, delete the delete marker to bring your objects back to the bucket.
Console.WriteLine("Removing the delete markers .....");
var deleteObjectResponse = await client.DeleteObjectsAsync(multiObjectDeleteRequest);
Console.WriteLine($"Successfully deleted the
{
deleteObjectResponse.DeletedObjects.Count} delete markers");
catch (DeleteObjectsException ex)
DisplayDeletionErrors(ex);
/// <summary>
/// Create temporary Amazon S3 objects to show how object deletion wors in an
/// Amazon S3 bucket with versioning enabled.
/// </summary>
/// <param name="client">The initialized Amazon S3 client object used to call
/// PutObjectAsync to create temporary objects for the example.</param>
/// <param name="bucketName">A string representing the name of the S3
/// bucket where we will create the temporary objects.</param>
/// <param name="number">The number of temporary objects to create.</param>
/// <returns>A list of the KeyVersion objects.</returns>
private static async Task<List<KeyVersion>> PutObjectsAsync(IAmazonS3 client, string bucketName, int number)
var keys = new List<KeyVersion>();
for (var i = 0; i < number; i++)
string key = "ObjectToDelete-" + new System.Random().Next();
PutObjectRequest request = new PutObjectRequest
BucketName = bucketName,
Key = key,
ContentBody = "This is the content body!",
var response = await client.PutObjectAsync(request);
KeyVersion keyVersion = new KeyVersion
Key = key,
VersionId = response.VersionId,
keys.Add(keyVersion);
return keys;
###############################################################################
# function errecho
# This function outputs everything sent to it to STDERR (standard error output).
###############################################################################
function errecho() {
printf "%s\n" "$*" 1>&2
###############################################################################
# function delete_items_in_bucket
# This function deletes the specified list of keys from the specified bucket.
# Parameters:
# $1 - The name of the bucket.
# $2 - A list of keys in the bucket to delete.
# Returns:
# 0 - If successful.
# 1 - If it fails.
###############################################################################
function delete_items_in_bucket() {
local bucket_name=$1
local keys=$2
local response
# Create the JSON for the items to delete.
local delete_items
delete_items="{\"Objects\":["
for key in $keys; do
delete_items="$delete_items{\"Key\": \"$key\"},"
delete_items=${delete_items%?} # Remove the final comma.
delete_items="$delete_items]}"
response=$(aws s3api delete-objects \
--bucket "$bucket_name" \
--delete "$delete_items")
# shellcheck disable=SC2181
if [[ $? -ne 0 ]]; then
errecho "ERROR: AWS reports s3api delete-object operation failed.\n$response"
return 1
bool AwsDoc::S3::deleteObjects(const std::vector<Aws::String> &objectKeys,
const Aws::String &fromBucket,
const Aws::S3::S3ClientConfiguration &clientConfig) {
Aws::S3::S3Client client(clientConfig);
Aws::S3::Model::DeleteObjectsRequest request;
Aws::S3::Model::Delete deleteObject;
for (const Aws::String &objectKey: objectKeys) {
deleteObject.AddObjects(Aws::S3::Model::ObjectIdentifier().WithKey(objectKey));
request.SetDelete(deleteObject);
request.SetBucket(fromBucket);
Aws::S3::Model::DeleteObjectsOutcome outcome =
client.DeleteObjects(request);
if (!outcome.IsSuccess()) {
auto err = outcome.GetError();
std::cerr << "Error deleting objects. " <<
err.GetExceptionName() << ": " << err.GetMessage() << std::endl;
} else {
std::cout << "Successfully deleted the objects.";
for (size_t i = 0; i < objectKeys.size(); ++i) {
std::cout << objectKeys[i];
if (i < objectKeys.size() - 1) {
std::cout << ", ";
std::cout << " from bucket " << fromBucket << "." << std::endl;
return outcome.IsSuccess();
"DeleteMarkerVersionId": "mYAT5Mc6F7aeUL8SS7FAAqUPO1koHwzU",
"Key": "test1.txt",
"DeleteMarker": true
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/aws/smithy-go"
// S3Actions wraps S3 service actions.
type S3Actions struct {
S3Client *s3.Client
S3Manager *manager.Uploader
// DeleteObjects deletes a list of objects from a bucket.
func (actor S3Actions) DeleteObjects(ctx context.Context, bucket string, objects []types.ObjectIdentifier, bypassGovernance bool) error {
if len(objects) == 0 {
return nil
input := s3.DeleteObjectsInput{
Bucket: aws.String(bucket),
Delete: &types.Delete{
Objects: objects,
Quiet: aws.Bool(true),
if bypassGovernance {
input.BypassGovernanceRetention = aws.Bool(true)
delOut, err := actor.S3Client.DeleteObjects(ctx, &input)
if err != nil || len(delOut.Errors) > 0 {
log.Printf("Error deleting objects from bucket %s.\n", bucket)
if err != nil {
var noBucket *types.NoSuchBucket
if errors.As(err, &noBucket) {
log.Printf("Bucket %s does not exist.\n", bucket)
err = noBucket
} else if len(delOut.Errors) > 0 {
for _, outErr := range delOut.Errors {
log.Printf("%s: %s\n", *outErr.Key, *outErr.Message)
err = fmt.Errorf("%s", *delOut.Errors[0].Message)
} else {
for _, delObjs := range delOut.Deleted {
err = s3.NewObjectNotExistsWaiter(actor.S3Client).Wait(
ctx, &s3.HeadObjectInput{Bucket: aws.String(bucket), Key: delObjs.Key}, time.Minute)
if err != nil {
log.Printf("Failed attempt to wait for object %s to be deleted.\n", *delObjs.Key)
} else {
log.Printf("Deleted %s.\n", *delObjs.Key)
return err
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import software.amazon.awssdk.services.s3.model.ObjectIdentifier;
import software.amazon.awssdk.services.s3.model.Delete;
import software.amazon.awssdk.services.s3.model.DeleteObjectsRequest;
import software.amazon.awssdk.services.s3.model.S3Exception;
import java.util.ArrayList;
* Before running this Java V2 code example, set up your development
* environment, including your credentials.
* For more information, see the following documentation topic:
* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
public class DeleteMultiObjects {
public static void main(String[] args) {
final String usage = """
Usage: <bucketName>
Where:
bucketName - the Amazon S3 bucket name.
if (args.length != 1) {
System.out.println(usage);
System.exit(1);
String bucketName = args[0];
Region region = Region.US_EAST_1;
S3Client s3 = S3Client.builder()
.region(region)
.build();
deleteBucketObjects(s3, bucketName);
s3.close();
* Deletes multiple objects from an Amazon S3 bucket.
* @param s3 An Amazon S3 client object.
* @param bucketName The name of the Amazon S3 bucket to delete objects from.
public static void deleteBucketObjects(S3Client s3, String bucketName) {
// Upload three sample objects to the specfied Amazon S3 bucket.
ArrayList<ObjectIdentifier> keys = new ArrayList<>();
PutObjectRequest putOb;
ObjectIdentifier objectId;
for (int i = 0; i < 3; i++) {
String keyName = "delete object example " + i;
objectId = ObjectIdentifier.builder()
.key(keyName)
.build();
putOb = PutObjectRequest.builder()
.bucket(bucketName)
.key(keyName)
.build();
s3.putObject(putOb, RequestBody.fromString(keyName));
keys.add(objectId);
System.out.println(keys.size() + " objects successfully created.");
// Delete multiple objects in one request.
Delete del = Delete.builder()
.objects(keys)
.build();
try {
DeleteObjectsRequest multiObjectDeleteRequest = DeleteObjectsRequest.builder()
.bucket(bucketName)
.delete(del)
.build();
s3.deleteObjects(multiObjectDeleteRequest);
System.out.println("Multiple objects are deleted!");
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
* Delete multiple objects from an S3 bucket.
* @param {{ bucketName: string, keys: string[] }}
export const main = async ({ bucketName, keys }) => {
const client = new S3Client({});
try {
const { Deleted } = await client.send(
new DeleteObjectsCommand({
Bucket: bucketName,
Delete: {
Objects: keys.map((k) => ({ Key: k })),
for (const key in keys) {
await waitUntilObjectNotExists(
{ client },
{ Bucket: bucketName, Key: key },
console.log(
`Successfully deleted ${Deleted.length} objects from S3 bucket. Deleted objects:`,
console.log(Deleted.map((d) => ` • ${d.Key}`).join("\n"));
} catch (caught) {
caught instanceof S3ServiceException &&
caught.name === "NoSuchBucket"
console.error(
`Error from S3 while deleting objects from ${bucketName}. The bucket doesn't exist.`,
} else if (caught instanceof S3ServiceException) {
console.error(
`Error from S3 while deleting objects from ${bucketName}. ${caught.name}: ${caught.message}`,
} else {
throw caught;
S3Client.fromEnvironment { region = "us-east-1" }.use { s3 ->
s3.deleteObjects(request)
println("$objectName was deleted from $bucketName")
'Bucket' => $this->bucketName,
if (isset($check['Contents']) && count($check['Contents']) > 0) {
throw new Exception("Bucket wasn't empty.");
echo "Deleted all objects and folders from $this->bucketName.\n";
} catch (Exception $exception) {
echo "Failed to delete $fileName from $this->bucketName with error: " . $exception->getMessage();
exit("Please fix error with object deletion before continuing.");
$keyVersions = @()
$markers = (Get-S3Version -BucketName $BucketName).Versions | Where {$_.IsDeleteMarker -eq "True"}
foreach ($marker in $markers) { $keyVersions += @{ Key = $marker.Key; VersionId = $marker.VersionId } }
Remove-S3Object -BucketName $BucketName -KeyAndVersionCollection $keyVersions -Force
$keyVersions = @()
$markers = (Get-S3Version -BucketName $BucketName).Versions | Where {$_.IsDeleteMarker -eq "True"}
foreach ($marker in $markers) { $keyVersions += @{ Key = $marker.Key; VersionId = $marker.VersionId } }
Remove-S3Object -BucketName $BucketName -KeyAndVersionCollection $keyVersions -Force
def __init__(self, s3_object):
:param s3_object: A Boto3 Object resource. This is a high-level resource in Boto3
that wraps object actions in a class-like structure.
self.object = s3_object
self.key = self.object.key
@staticmethod
def delete_objects(bucket, object_keys):
Removes a list of objects from a bucket.
This operation is done as a batch in a single request.
:param bucket: The bucket that contains the objects. This is a Boto3 Bucket
resource.
:param object_keys: The list of keys that identify the objects to remove.
:return: The response that contains data about which objects were deleted
and any that could not be deleted.
response = bucket.delete_objects(
Delete={"Objects": [{"Key": key} for key in object_keys]}
if "Deleted" in response:
logger.info(
"Deleted objects '%s' from bucket '%s'.",
[del_obj["Key"] for del_obj in response["Deleted"]],
bucket.name,
if "Errors" in response:
logger.warning(
"Could not delete objects '%s' from bucket '%s'.",
f"{del_obj['Key']}: {del_obj['Code']}"
for del_obj in response["Errors"]
bucket.name,
except ClientError:
logger.exception("Couldn't delete any objects from bucket %s.", bucket.name)
raise
else:
return response
def __init__(self, s3_object):
:param s3_object: A Boto3 Object resource. This is a high-level resource in Boto3
that wraps object actions in a class-like structure.
self.object = s3_object
self.key = self.object.key
@staticmethod
def empty_bucket(bucket):
Remove all objects from a bucket.
:param bucket: The bucket to empty. This is a Boto3 Bucket resource.
bucket.objects.delete()
logger.info("Emptied bucket '%s'.", bucket.name)
except ClientError:
logger.exception("Couldn't empty bucket '%s'.", bucket.name)
raise
def permanently_delete_object(bucket, object_key):
Permanently deletes a versioned object by deleting all of its versions.
Usage is shown in the usage_demo_single_object function at the end of this module.
:param bucket: The bucket that contains the object.
:param object_key: The object to delete.
bucket.object_versions.filter(Prefix=object_key).delete()
logger.info("Permanently deleted all versions of object %s.", object_key)
except ClientError:
logger.exception("Couldn't delete all versions of %s.", object_key)
raise
# Deletes the objects in an Amazon S3 bucket and deletes the bucket.
# @param bucket [Aws::S3::Bucket] The bucket to empty and delete.
def delete_bucket(bucket)
puts("\nDo you want to delete all of the objects as well as the bucket (y/n)? ")
answer = gets.chomp.downcase
if answer == 'y'
bucket.objects.batch_delete!
bucket.delete
puts("Emptied and deleted bucket #{bucket.name}.\n")
rescue Aws::Errors::ServiceError => e
puts("Couldn't empty and delete bucket #{bucket.name}.")
puts("\t#{e.code}: #{e.message}")
raise
/// Delete the objects in a bucket.
pub async fn delete_objects(
client: &aws_sdk_s3::Client,
bucket_name: &str,
objects_to_delete: Vec<String>,
) -> Result<(), S3ExampleError> {
// Push into a mut vector to use `?` early return errors while building object keys.
let mut delete_object_ids: Vec<aws_sdk_s3::types::ObjectIdentifier> = vec![];
for obj in objects_to_delete {
let obj_id = aws_sdk_s3::types::ObjectIdentifier::builder()
.key(obj)
.build()
.map_err(|err| {
S3ExampleError::new(format!("Failed to build key for delete_object: {err:?}"))
delete_object_ids.push(obj_id);
client
.delete_objects()
.bucket(bucket_name)
.delete(
aws_sdk_s3::types::Delete::builder()
.set_objects(Some(delete_object_ids))
.build()
.map_err(|err| {
S3ExampleError::new(format!("Failed to build delete_object input {err:?}"))
.send()
.await?;
Ok(())
public func deleteObjects(bucket: String, keys: [String]) async throws {
let input = DeleteObjectsInput(
bucket: bucket,
delete: S3ClientTypes.Delete(
objects: keys.map { S3ClientTypes.ObjectIdentifier(key: $0) },
quiet: true
_ = try await client.deleteObjects(input: input)
} catch {
print("ERROR: deleteObjects:", dump(error))
throw error