Nyxynyx
Nyxynyx

Reputation: 63619

Delete folder in Amazon S3 using PHP

I just started trying out Amazon S3 for hosting my website's images. I'm using the official Amazon AWS PHP SDK library.

Problem: How can I delete all files located in a S3 'folder'?
For example if I have a file named images/2012/photo.jpg, I want to delete all files whose filenames start with images/2012/.

Upvotes: 13

Views: 17188

Answers (5)

Luigi C.
Luigi C.

Reputation: 1060

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');

Please consider the comment of @FranciscoCarmona which I report here to give evidence:

Just note one thing. If you have a file named dirXXX.jpg within "some" directory that will remove the file too. So that is not correct. You will have to add a regex. $s3->deleteMatchingObjects('YOUR_BUCKET_NAME', '/some/dir', '/^some/dir//');

Upvotes: 35

HackerBoy
HackerBoy

Reputation: 11

$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(); 

Upvotes: 1

adatadoc
adatadoc

Reputation: 349

I've tested this and it works 2019-05-28

function Amazon_s3_delete_dir($delPath, $s3, $bucket) {
//the $dir is the path to the directory including the directory
// the directories need to have a / at the end.  
// Clear it just in case it may or may not be there and then add it back in.
$dir = rtrim($dir, "/");
$dir = ltrim($dir, "/");
$dir = $dir . "/";

$response = $s3->getIterator(
        'ListObjects',
        [
            'Bucket' => $bucket,
            'Prefix' => $delPath
        ]
);
//delete each 
foreach ($response as $object) {
    $fileName = $object['Key'];
    $s3->deleteObject([
        'Bucket' => $bucket,
        'Key' => $fileName
    ]);
}//foreach

    return true;
 }//function

Usage:

$delPath = $myDir . $theFolderName . "/";        
Amazon_s3_delete_dir($delPath, $s3, $bucket);

Upvotes: 0

nategood
nategood

Reputation: 12005

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).

Upvotes: 10

Jason
Jason

Reputation: 2727

Here is a function that will do what you are looking to do.

/**
*   This function will delete a directory.  It first needs to look up all objects with the specified directory
*   and then delete the objects.
*/
function Amazon_s3_delete_dir($dir){
    $s3 = new AmazonS3();

    //the $dir is the path to the directory including the directory

    // the directories need to have a / at the end.  
    // Clear it just in case it may or may not be there and then add it back in.
            $dir = rtrim($dir, "/");
            $dir = ltrim($dir, "/");
            $dir = $dir . "/";

    //get list of directories
        $response = $s3->get_object_list(YOUR_A3_BUCKET, array(
           'prefix' => $dir
        ));


    //delete each 
        foreach ($response as $v) {
            $s3->delete_object(YOUR_A3_BUCKET, $v);
        }//foreach

    return true;

}//function

Use: if I want to delete the directory foo

Amazon_s3_delete_dir("path/to/directory/foo/");

Upvotes: 0

Related Questions