PHP Script to Delete from Supabase Storage

Here’s a clean, actionable PHP script that connects to an S3-compatible storage system (e.g., AWS S3, MinIO, DigitalOcean Spaces, Supabase Storage with S3 API enabled) and deletes objects older than 6 months.

It uses the official AWS SDK for PHP (aws/aws-sdk-php), which works with any S3-compatible endpoint.


📜 PHP Script

<?php
require 'vendor/autoload.php';

use Aws\S3\S3Client;
use Aws\Exception\AwsException;

// Configure your S3-compatible storage
$s3 = new S3Client([
    'version'     => 'latest',
    'region'      => 'us-west-1', // adjust if needed
    'endpoint'    => '', // e.g. https://s3.amazonaws.com or https://nyc3.digitaloceanspaces.com
    'use_path_style_endpoint' => true, // often required for non-AWS S3
    'credentials' => [
        'key'    => '',
        'secret' => '',
    ],
]);

$bucket = 'faceless-images';

// Calculate cutoff date (12 months ago in your example)
$cutoff = new DateTime();
$cutoff->modify('-12 months');

try {
    // List all objects in the bucket
    $objects = $s3->getPaginator('ListObjectsV2', [
        'Bucket' => $bucket,
    ]);

    foreach ($objects as $page) {
        if (!isset($page['Contents'])) {
            continue;
        }

        foreach ($page['Contents'] as $object) {
            $key = $object['Key'];

            try {
                // Fetch object metadata
                $head = $s3->headObject([
                    'Bucket' => $bucket,
                    'Key'    => $key,
                ]);

                $meta = $head['Metadata'];

                if (isset($meta['created-at'])) {
                    $createdAt = new DateTime($meta['created-at']);

                    if ($createdAt < $cutoff) {
                        echo "Deleting: {$key} (Created at: {$createdAt->format('Y-m-d')})\n";

                        $s3->deleteObject([
                            'Bucket' => $bucket,
                            'Key'    => $key,
                        ]);
                    }
                } else {
                    // Fallback: if no created-at metadata, you could skip or use LastModified
                    // $lastModified = new DateTime($object['LastModified']);
                }

            } catch (AwsException $e) {
                echo "Error fetching metadata for {$key}: " . $e->getMessage() . "\n";
            }
        }
    }

    echo "Cleanup complete.\n";

} catch (AwsException $e) {
    echo "Error: " . $e->getMessage() . "\n";
}

⚙️ How It Works

  1. Connects to your S3-compatible endpoint with credentials.
  2. Lists all objects in the bucket (paged).
  3. Compares each object’s LastModified timestamp against the cutoff (6 months ago).
  4. Deletes files older than 6 months.

✅ Notes

  • Install the AWS SDK with: composer require aws/aws-sdk-php
  • Replace endpoint, key, secret, and bucket with your actual values.
  • If you’re using AWS S3 itself, you can omit the endpoint and use_path_style_endpoint options.
  • For Supabase Storage, you’d need to enable the S3-compatible API and use the provided endpoint/keys.

Leave a Reply