Skip to content

Commit

Permalink
JavaScript (v3): S3 - Add 'delete all objects' scenario. (awsdocs#6812)
Browse files Browse the repository at this point in the history
  • Loading branch information
cpyle0819 authored Sep 4, 2024
1 parent fb196fe commit 3eb8b33
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .doc_gen/metadata/s3_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3392,3 +3392,20 @@ s3_Scenario_ProcessS3EventNotification:
services:
s3: {PutBucketNotificationConfiguration}
sqs: {ReceiveMessage, GetQueueAttributes, DeleteMessageBatch}
s3_Scenario_DeleteAllObjects:
title: Delete all objects in a given &S3; bucket using an &AWS; SDK.
title_abbrev: Delete all objects in a bucket
synopsis: delete all of the objects in an &S3; bucket.
category: Scenarios
languages:
JavaScript:
versions:
- sdk_version: 3
github: javascriptv3/example_code/s3
sdkguide:
excerpts:
- description: Delete all objects for a given &S3; bucket.
snippet_files:
- javascriptv3/example_code/s3/scenarios/delete-all-objects.js
services:
s3: {DeleteObjects, ListObjectsV2}
13 changes: 13 additions & 0 deletions javascriptv3/example_code/s3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ functions within the same service.

- [Create a presigned URL](scenarios/presigned-url-upload.js)
- [Create a web page that lists Amazon S3 objects](../web/s3/list-objects/src/App.tsx)
- [Delete all objects in a bucket](scenarios/delete-all-objects.js)
- [Get the legal hold configuration of an object](actions/get-object-legal-hold.js)
- [Lock Amazon S3 objects](scenarios/object-locking/index.js)
- [Upload or download large files](scenarios/multipart-upload.js)
Expand Down Expand Up @@ -175,6 +176,18 @@ This example shows you how to list Amazon S3 objects in a web page.
<!--custom.scenarios.s3_Scenario_ListObjectsWeb.start-->
<!--custom.scenarios.s3_Scenario_ListObjectsWeb.end-->

#### Delete all objects in a bucket

This example shows you how to delete all of the objects in an Amazon S3 bucket.


<!--custom.scenario_prereqs.s3_Scenario_DeleteAllObjects.start-->
<!--custom.scenario_prereqs.s3_Scenario_DeleteAllObjects.end-->


<!--custom.scenarios.s3_Scenario_DeleteAllObjects.start-->
<!--custom.scenarios.s3_Scenario_DeleteAllObjects.end-->

#### Get the legal hold configuration of an object

This example shows you how to get the legal hold configuration of an S3 bucket.
Expand Down
60 changes: 60 additions & 0 deletions javascriptv3/example_code/s3/scenarios/delete-all-objects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

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 "url";
import { parseArgs } from "util";
if (process.argv[1] === fileURLToPath(import.meta.url)) {
const options = {
bucketName: {
type: "string",
},
};

const { values } = parseArgs({ options });
main(values);
}

0 comments on commit 3eb8b33

Please sign in to comment.