How to delete files recursively from an S3 bucket

后端 未结 12 489
不知归路
不知归路 2021-01-29 23:54

I have the following folder structure in S3. Is there a way to recursively remove all files under a certain folder (say foo/bar1 or foo or foo/bar2/1 ..)

         


        
12条回答
  •  爱一瞬间的悲伤
    2021-01-30 00:38

    Best way is to use lifecycle rule to delete whole bucket contents. Programmatically you can use following code (PHP) to PUT lifecycle rule.

    $expiration = array('Date' => date('U', strtotime('GMT midnight')));
    $result = $s3->putBucketLifecycle(array(
                'Bucket' => 'bucket-name',
                'Rules' => array(
                    array(
                        'Expiration' => $expiration,
                        'ID' => 'rule-name',
                        'Prefix' => '',
                        'Status' => 'Enabled',
                    ),
                ),
            ));
    

    In above case all the objects will be deleted starting Date - "Today GMT midnight".

    You can also specify Days as follows. But with Days it will wait for at least 24 hrs (1 day is minimum) to start deleting the bucket contents.

    $expiration = array('Days' => 1);
    

提交回复
热议问题