2
votes

I'm using aws/aws-sdk-php-laravel to create buckets and store content. With each bucket created, I add a lifecycle using putBucketLifecycle() to have it automatically deleted after 7 days, like this:

$s3->putBucketLifecycle([
    'Bucket'    => $bucket,
    'Rules'     => [
        [
            'Expiration' => 7,
            'ID'         => 'expires-after-7-days--' . uniqid(),
            'Prefix'     => '',
            'Status'     => 'Enabled',
        ],
    ],
]);

And it works well... sort of. After 7 days, in fact, all files in the bucket are cleared. But strange enough, the bucket itself persists. As a result, my S3 is now full of empty buckets:

Empty bucketsImage source

So the question is, how can I set the buckets to remove themselves also? Am I missing some paremeter/configuration? Or need I call deleteBucket() explicitly?

2

2 Answers

0
votes

Buckets do not delete themselves. Lifecycle policies only apply to objects in the bucket, in spite of the name "bucket lifecycle."

0
votes

Yes you can only delete objects inside bucket using putBucketLifecycle() as Lifecycle rule applies to the objects inside bucket.

Once all the objects inside bucket are deleted (after expiration) you can use following calls to delete the bucket

// check if bucket is empty
$s3->listObjects(...);
// or use ListObjects iterator
$s3->getIterator('ListObjects', array('Bucket' => $bucket));
// delete bucket if its empty
$s3->deleteBucket(...);