Delete all objects in a given Amazon S3 bucket using an AWS SDK - Amazon Simple Storage Service

Delete all objects in a given Amazon S3 bucket using an AWS SDK

The following code example shows how to delete all of the objects in an Amazon S3 bucket.

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 AWS Code Examples Repository.

Delete all objects for a given Amazon S3 bucket.

import { DeleteObjectsCommand, paginateListObjectsV2, S3Client, } from "@aws-sdk/client-s3"; /** * * @param {{ bucketName: string }} config */ export const main = async ({ bucketName }) => { const client = new S3Client({}); try { console.log(`Deleting all objects in bucket: ${bucketName}`); const paginator = paginateListObjectsV2( { client }, { Bucket: bucketName, }, ); const objectKeys = []; for await (const { Contents } of paginator) { objectKeys.push(...Contents.map((obj) => ({ Key: obj.Key }))); } const deleteCommand = new DeleteObjectsCommand({ Bucket: bucketName, Delete: { Objects: objectKeys }, }); await client.send(deleteCommand); console.log(`All objects deleted from bucket: ${bucketName}`); } catch (caught) { if (caught instanceof Error) { console.error( `Failed to empty ${bucketName}. ${caught.name}: ${caught.message}`, ); } } }; // Call function if run directly. import { fileURLToPath } from "node:url"; import { parseArgs } from "node:util"; if (process.argv[1] === fileURLToPath(import.meta.url)) { const options = { bucketName: { type: "string", }, }; const { values } = parseArgs({ options }); main(values); }

For a complete list of AWS SDK developer guides and code examples, see Developing with Amazon S3 using the AWS SDKs. This topic also includes information about getting started and details about previous SDK versions.