Skip to content

Commit

Permalink
chore: improve delete-buckets to delete delete-markers
Browse files Browse the repository at this point in the history
  • Loading branch information
OlafConijn committed Nov 18, 2023
1 parent 5056992 commit d8b5280
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions test/integration-tests/tools/delete-buckets.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import { DeleteBucketCommand, DeleteObjectCommand, ListBucketsCommand, ListObjectsCommand, S3Client } from '@aws-sdk/client-s3';
import { DeleteBucketCommand, DeleteObjectCommand, ListBucketsCommand, ListObjectVersionsCommand, ListObjectsCommand, ListObjectsV2Command, S3Client } from '@aws-sdk/client-s3';
import { fromIni } from '@aws-sdk/credential-providers';

const bucketsToDelete = /(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}/;

const deleteBuckets = async (): Promise<void> => {
const s3 = new S3Client({ credentials: fromIni({ profile: 'org-formation-test-v2' }) });
const s3 = new S3Client({ credentials: fromIni({ profile: 'org-formation-test-v2' }), region: "eu-west-1" });
const bucketList = await s3.send(new ListBucketsCommand({}));
for (const bucket of bucketList.Buckets) {

if (bucketsToDelete.test(bucket.Name)) {
console.log(`deleting ${bucket.Name}`);
try {
const objectList = await s3.send(new ListObjectsCommand({ Bucket: bucket.Name }));
for (const obj of objectList.Contents) {
await s3.send(new DeleteObjectCommand({ Bucket: bucket.Name, Key: obj.Key }));
const objectVersionList = await s3.send(new ListObjectVersionsCommand({ Bucket: bucket.Name }));
for (const obj of (objectVersionList.Versions || [])) {
await s3.send(new DeleteObjectCommand({ Bucket: bucket.Name, Key: obj.Key, VersionId: obj.VersionId }));
}
for (const obj of (objectVersionList.DeleteMarkers || [])) {
await s3.send(new DeleteObjectCommand({ Bucket: bucket.Name, Key: obj.Key, VersionId: obj.VersionId }));
}
await s3.send(new DeleteBucketCommand({ Bucket: bucket.Name }));
} catch (err) {
Expand Down

0 comments on commit d8b5280

Please sign in to comment.