There are more AWS SDK examples available in the AWS Doc SDK Examples
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 API details, see the following topics in AWS SDK for JavaScript API Reference.
-
Create an Amazon Textract explorer application
Delete incomplete multipart uploads