Skip to content

Instantly share code, notes, and snippets.

@vishwarajanand
Created December 14, 2023 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vishwarajanand/38a1a70b4471e3d21ae2d97b28dc1736 to your computer and use it in GitHub Desktop.
Save vishwarajanand/38a1a70b4471e3d21ae2d97b28dc1736 to your computer and use it in GitHub Desktop.
Delete buckets (and all objects inside it) with a certain prefix name from Google Cloud Storage
<?php
require_once __DIR__ . '/../vendor/autoload.php';
## Delete all buckets (and objects inside it) with bucket name having prefix: `gcloud_testing_`
use Google\Cloud\Storage\StorageClient;
$client = new StorageClient();
$buckets = $client->buckets();
$countBucket = 0;
$countObject = 0;
foreach ($buckets as $bucket) {
echo $bucket->name() . "\n";
if (str_starts_with($bucket->name(), 'gcloud_testing_')) {
$countBucket += 1;
// Delete all objects in the bucket
$objects = $bucket->objects();
foreach ($objects as $object) {
$countObject += 1;
$object->delete();
}
$bucket->delete();
}
}
echo "Deleted $countBucket buckets and $countObject objects.\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment