Delete multiple objects from an Amazon S3 bucket using an Amazon SDK - Amazon Simple Storage Service
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China.

Delete multiple objects from an Amazon S3 bucket using an Amazon SDK

The following code examples show how to delete multiple objects from an S3 bucket.

.NET
Amazon SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Delete all objects in an S3 bucket.

/// <summary> /// Delete all of the objects stored in an existing Amazon S3 bucket. /// </summary> /// <param name="client">An initialized Amazon S3 client object.</param> /// <param name="bucketName">The name of the bucket from which the /// contents will be deleted.</param> /// <returns>A boolean value that represents the success or failure of /// deleting all of the objects in the bucket.</returns> public static async Task<bool> DeleteBucketContentsAsync(IAmazonS3 client, string bucketName) { // Iterate over the contents of the bucket and delete all objects. var request = new ListObjectsV2Request { BucketName = bucketName, }; try { var response = await client.ListObjectsV2Async(request); do { 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; } }
  • For API details, see DeleteObjects in Amazon SDK for .NET API Reference.

Java
SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

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); } }
  • For API details, see DeleteObjects in Amazon SDK for Java 2.x API Reference.

JavaScript
SDK for JavaScript V3
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Create the client.

// Create service client module using ES6 syntax. import { S3Client } from "@aws-sdk/client-s3"; // Set the AWS Region. const REGION = "us-east-1"; // Create an Amazon S3 service client object. const s3Client = new S3Client({ region: REGION }); export { s3Client };

Delete multiple objects.

import { DeleteObjectsCommand } from "@aws-sdk/client-s3"; import { s3Client } from "./libs/s3Client.js" // Helper function that creates an Amazon S3 service client module. export const bucketParams = { Bucket: "BUCKET_NAME", Delete: { Objects: [ { Key: "KEY_1", }, { Key: "KEY_2", }, ], }, }; export const run = async () => { try { const data = await s3Client.send(new DeleteObjectsCommand(bucketParams)); return data; // For unit tests. console.log("Success. Object deleted."); } catch (err) { console.log("Error", err); } }; run();

Delete all objects in a bucket.

import { ListObjectsCommand, DeleteObjectCommand } from "@aws-sdk/client-s3"; import { s3Client } from "./libs/s3Client.js"; // Helper function that creates an Amazon S3 service client module. export const bucketParams = { Bucket: "BUCKET_NAME" }; export const run = async () => { try { const data = await s3Client.send(new ListObjectsCommand(bucketParams)); let noOfObjects = data.Contents; for (let i = 0; i < noOfObjects.length; i++) { await s3Client.send( new DeleteObjectCommand({ Bucket: bucketParams.Bucket, Key: noOfObjects[i].Key, }) ); } console.log("Success. Objects deleted."); return data; // For unit tests. } catch (err) { console.log("Error", err); } }; run();
  • For API details, see DeleteObjects in Amazon SDK for JavaScript API Reference.

Kotlin
SDK for Kotlin
Note

This is prerelease documentation for a feature in preview release. It is subject to change.

Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

suspend fun deleteBucketObjects(bucketName: String, objectName: String) { val objectId = ObjectIdentifier { key = objectName } val delOb = Delete { objects = listOf(objectId) } val request = DeleteObjectsRequest { bucket = bucketName delete = delOb } S3Client { region = "us-east-1" }.use { s3 -> s3.deleteObjects(request) println("$objectName was deleted from $bucketName") } }
  • For API details, see DeleteObjects in Amazon SDK for Kotlin API reference.

PHP
SDK for PHP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Delete a set of objects from a list of keys.

$s3client = new Aws\S3\S3Client(['region' => 'us-west-2', 'version' => 'latest']); try { $objects = []; foreach ($contents['Contents'] as $content) { $objects[] = [ 'Key' => $content['Key'], ]; } $s3client->deleteObjects([ 'Bucket' => $bucket_name, 'Key' => $file_name, 'Delete' => [ 'Objects' => $objects, ], ]); $check = $s3client->listObjects([ 'Bucket' => $bucket_name, ]); if (count($check) <= 0) { throw new Exception("Bucket wasn't empty."); } echo "Deleted all objects and folders from $bucket_name.\n"; } catch (Exception $exception) { echo "Failed to delete $file_name from $bucket_name with error: " . $exception->getMessage(); exit("Please fix error with object deletion before continuing."); }
  • For API details, see DeleteObjects in Amazon SDK for PHP API Reference.

Python
SDK for Python (Boto3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Delete a set of objects by using a list of object keys.

class ObjectWrapper: """Encapsulates S3 object actions.""" 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. """ try: 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

Delete all objects in a bucket.

class ObjectWrapper: """Encapsulates S3 object actions.""" 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. """ try: bucket.objects.delete() logger.info("Emptied bucket '%s'.", bucket.name) except ClientError: logger.exception("Couldn't empty bucket '%s'.", bucket.name) raise

Permanently delete a versioned object by deleting all of its versions.

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. """ try: 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
  • For API details, see DeleteObjects in Amazon SDK for Python (Boto3) API Reference.

Ruby
SDK for Ruby
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

# 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") end rescue Aws::Errors::ServiceError => e puts("Couldn't empty and delete bucket #{bucket.name}.") puts("\t#{e.code}: #{e.message}") raise end
  • For API details, see DeleteObjects in Amazon SDK for Ruby API Reference.

Rust
SDK for Rust
Note

This documentation is for an SDK in preview release. The SDK is subject to change and should not be used in production.

Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

pub async fn delete_objects(client: &Client, bucket_name: &str) -> Result<(), Error> { let objects = client.list_objects_v2().bucket(bucket_name).send().await?; let mut delete_objects: Vec<ObjectIdentifier> = vec![]; for obj in objects.contents().unwrap_or_default() { let obj_id = ObjectIdentifier::builder() .set_key(Some(obj.key().unwrap().to_string())) .build(); delete_objects.push(obj_id); } client .delete_objects() .bucket(bucket_name) .delete(Delete::builder().set_objects(Some(delete_objects)).build()) .send() .await?; let objects: ListObjectsV2Output = client.list_objects_v2().bucket(bucket_name).send().await?; match objects.key_count { 0 => Ok(()), _ => Err(Error::Unhandled(Box::from( "There were still objects left in the bucket.", ))), } }
  • For API details, see DeleteObjects in Amazon SDK for Rust API reference.

Swift
SDK for Swift
Note

This is prerelease documentation for an SDK in preview release. It is subject to change.

Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

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 ) ) do { _ = try await client.deleteObjects(input: input) } catch { throw error } }
  • For API details, see DeleteObjects in Amazon SDK for Swift API reference.

For a complete list of Amazon SDK developer guides and code examples, see Using this service with an Amazon SDK. This topic also includes information about getting started and details about previous SDK versions.