Created
December 14, 2023 13:50
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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