Skip to content

Commit

Permalink
feat(truncate): improve object deletion in Everland bucket by batchin…
Browse files Browse the repository at this point in the history
…g deletes and enhancing logging for truncation process
  • Loading branch information
bigint committed Sep 3, 2024
1 parent e2b7de6 commit 9c5e25f
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions apps/cron/src/truncate4EverlandBucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,43 @@ const truncate4EverlandBucket = async () => {

if (Contents) {
const oldObjects = Contents.filter(
(object) => new Date(object.LastModified!) < dateDaysAgo
(object) =>
object.LastModified && new Date(object.LastModified) < dateDaysAgo
);
objectsToDelete = objectsToDelete.concat(
oldObjects.map((object) => ({ Key: object.Key! }))
oldObjects
.map((object) => ({ Key: object.Key! }))
.filter((obj) => obj.Key)
);
}

ContinuationToken = IsTruncated ? NextContinuationToken : undefined;
} while (ContinuationToken);

console.log(objectsToDelete.length);
logger.info(
`[Cron] truncate4EverlandBucket - Found ${objectsToDelete.length} objects older than ${daysToSubtract} days.`
);

if (objectsToDelete.length > 0) {
const deleteCommand = new DeleteObjectsCommand({
Bucket,
Delete: { Objects: objectsToDelete }
});
const deleteBatchSize = 1000;

while (objectsToDelete.length > 0) {
const batch = objectsToDelete.splice(0, deleteBatchSize);

const deleteCommand = new DeleteObjectsCommand({
Bucket,
Delete: { Objects: batch }
});

s3Client.send(deleteCommand);
logger.info(
`[Cron] truncate4EverlandBucket - Deleted ${batch.length} objects in a batch.`
);
}

await s3Client.send(deleteCommand);
logger.info(
`[Cron] truncate4EverlandBucket - Deleted ${objectsToDelete.length} objects older than ${daysToSubtract} days.`
`[Cron] truncate4EverlandBucket - Deleted all objects older than ${daysToSubtract} days.`
);

return;
}

Expand Down

1 comment on commit 9c5e25f

@vercel
Copy link

@vercel vercel bot commented on 9c5e25f Sep 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

web – ./

web-heyxyz.vercel.app
web-git-main-heyxyz.vercel.app
hey.xyz
heyxyz.vercel.app

Please sign in to comment.