Skip to content

Instantly share code, notes, and snippets.

@vishwarajanand
Created December 14, 2023 13:44
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/48134c98c94e38cf3a32997dbc9d4a45 to your computer and use it in GitHub Desktop.
Save vishwarajanand/48134c98c94e38cf3a32997dbc9d4a45 to your computer and use it in GitHub Desktop.
Delete topics with a certain prefix name from Google PubSub
<?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