Created
December 14, 2023 13:44
-
-
Save vishwarajanand/48134c98c94e38cf3a32997dbc9d4a45 to your computer and use it in GitHub Desktop.
Delete topics with a certain prefix name from Google PubSub
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 topics and subscriptions with name having prefix: `gcloud_testing_` | |
use Google\Cloud\PubSub\PubSubClient; | |
$client = new PubSubClient(); | |
$topics = $client->topics(); | |
$countTopic = 0; | |
$countSubscription = 0; | |
foreach ($topics as $topic) { | |
if (str_contains($topic->name(), 'gcloud_testing_')) { | |
$countTopic += 1; | |
echo $topic->name() . "\n"; | |
// Delete all subscriptions in the topic | |
$subscriptions = iterator_to_array($topic->subscriptions()); | |
foreach ($subscriptions as $subscription) { | |
$countSubscription += 1; | |
if (str_contains($subscription->name(), 'gcloud_testing_')) { | |
$subscription->delete(); | |
} | |
} | |
$topic->delete(); | |
} | |
} | |
echo "Deleted $countTopic topics and $countSubscription subscriptions.\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment