PHP How To Delete Folder From AWS S3 Bucket

Reading Time: 6 minutes
3,557 Views

Inside this article we will see i.e PHP how to Delete Folder from AWS s3 bucket. There are very few simple steps by which we can remove files and folders from AWS s3 bucket.

AWS provides a composer package for PHP application. By the help of this package we can easily communicate with AWS s3 bucket with few keys and their APIs.

This article will give you the classified information about removing files, folders and subfolders from S3 bucket in PHP. If you are wondering to find the solution about to delete uploaded files and folders, then this article will help you a lot with AWS S3 bucket.

Learn More –

Let’s get started.

Install AWS PHP SDK

Before installation installation, please make sure these –

  • System should have composer installed
  • Project should have composer.json file

How To generate composer.json file

To generate a composer.json file. Open project into terminal and run this command.

$ composer init

You can provide either the information to generate else you can simply press Enter to pick default value.

So, here we have generate composer.json file with all default values in it.

Next,

Install AWS SDK.

$ composer require aws/aws-sdk-php

You will see like this –

It will create a vendor folder into your project folder.

Next,

Create Application To Delete Folder & Files

Create a file with any name like remove-object.php.

You need these while working with AWS S3 bucket.

  • AWS Access Key
  • AWS Secret Key
  • Bucket Name

Step #1 – AWS S3 Connection

// Instantiate an Amazon S3 client.
$s3Client = new S3Client([
    'version' => 'latest',
    'region'  => 'us-east-1', // Your AWS region
    'credentials' => [
        'key'    => $awsAccessKey,
        'secret' => $awsSecretKey
    ]
]);

Step #2 – Get Information of All Objects

// get Object all versions
$versions = $s3Client->listObjectVersions([
    'Bucket' => $bucket
])->getPath('Versions');

Step #3 – Get Information of a Specific Object

// get information
$versions = $s3Client->listObjectVersions([
    'Bucket' => $bucket,
    'Prefix' => 'your-folder-prefix/'
])->getPath('Versions');

Step #4 – Delete Each File / Folders From Object

// Deleting 1 by 1 each object version
foreach ($versions as $version) {
    $result = $s3Client->deleteObject([
        'Bucket' => $bucket,
        'Key' => $version['Key'],
        'VersionId' => $version['VersionId']
    ]);
}

Suppose, we have a upload/ folder into AWS S3 bucket. We want to delete all files and folders from this upload folder.

Open remove-object.php file and write this complete code into it.

<?php

require 'vendor/autoload.php';

use Aws\S3\S3Client;

$awsAccessKey = 'XXXX';
$awsSecretKey = 'YYYYYYYYYY';
$bucket = 'your-bucket-name';

// Instantiate an Amazon S3 client.
$s3Client = new S3Client([
    'version' => 'latest',
    'region'  => 'us-east-1', // Your AWS region
    'credentials' => [
        'key'    => $awsAccessKey,
        'secret' => $awsSecretKey
    ]
]);

// Use the high-level iterators (returns ALL of your objects).
try {

    // get Object all versions
    $versions = $s3Client->listObjectVersions([
        'Bucket' => $bucket,
        'Prefix' => 'upload/'
    ])->getPath('Versions');

    // Deleting 1 by 1 each object version
    foreach ($versions as $version) {
        $result = $s3Client->deleteObject([
            'Bucket' => $bucket,
            'Key' => $version['Key'],
            'VersionId' => $version['VersionId']
        ]);
    }
} catch (S3Exception $e) {
    
    echo $e->getMessage() . "\n";
}

The above code will remove all files, folders and subfolder from /upload folder what you have in your AWS s3 bucket.

We hope this article helped you to learn about PHP How To Delete Folder From AWS S3 Bucket in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.