Delete folder in Amazon S3 using PHP Delete folder in Amazon S3 using PHP codeigniter codeigniter

Delete folder in Amazon S3 using PHP


The best way to delete a folder from S3 with all its files is using the API deleteMatchingObjects()

$s3 = S3Client::factory(...);$s3->deleteMatchingObjects('YOUR_BUCKET_NAME', '/some/dir');


S3 does not have "folders" as you would traditionally think of them on a file system (some S3 clients just do a nice job making S3 appear to have folders). Those / are actually part of the file name.

As such, there is no "delete folder" option in the API. You would just need to delete each individual file that has the images/2012/... prefix.

Update:

This can be accomplished via the delete_all_objects method in the Amazon S3 PHP Client. Simply specify "/^images\/2012\//" as the regex prefix in the second argument (the first argument being your bucket name).


$s3 = new Aws\S3\Client([ 'region' => 'us-west-2', 'version' => 'latest' ]); $listObjectsParams = ['Bucket' => 'foo', 'Prefix' => 'starts/with/']; // Asynchronously delete $delete = Aws\S3\BatchDelete::fromListObjects($s3, $listObjectsParams); // Force synchronous completion $delete->delete();$promise = $delete->promise();