ukdriller
ukdriller

Reputation: 1

AWS Delete everything contained in a folder using the PHP SDK

I would like to know why my simple code, is not deleting from a bucket everthing contained in rave/fool

My code:

$result = $s3Client->deleteObjects([
    'Bucket' => 'mybucket',
    'Delete' => [
        'Objects' => [
            [
                'Key' => 'rave/fool/*',
            ],
            
        ],
        'Quiet' => true,
    ],
]);



It should delete every item contained in rave/fool/ cause I have a crazy amout of data that I would like to delete on a batch operation.

Thanks again, and great community.

Upvotes: 0

Views: 295

Answers (1)

jarmod
jarmod

Reputation: 78633

S3 does not have wildcards. The asterisk is being viewed as a literal character in a key (and presumably there is no object that matches that key).

You can list all objects with the relevant prefix (rave/fool/) and delete them one-by-one or in batches.

Update: it looks like the AWS PHP SDK includes a shortcut for this functionality: deleteMatchingObjects, but don't use the asterisk when you specify the prefix. You can also supply a regular expression and it will delete only objects that match the regex.

Upvotes: 3

Related Questions