When you create a subscription, you attach the subscription to a topic, and subscribers can receive messages from the subscription. To stop subscribers from receiving messages, you can detach subscriptions from the topic.
Before you detach a subscription, you need the pubsub.topics.detachSubscription
permission on the topic. You can detach a
subscription without permissions on the subscription, which is useful for
managing a topic that is in a different project than the subscription. For
more information, see Pub/Sub access control
.
Before you begin
- Learn about subscriptions .
- Create one of the following subscriptions, pull , push , or BigQuery .
Required roles and permissions
To get the permissions that
you need to detach subscriptions and manage them,
ask your administrator to grant you the Pub/Sub Editor
( roles/pubsub.editor
)
IAM role on your topic or project.
For more information about granting roles, see Manage access to projects, folders, and organizations
.
This predefined role contains the permissions required to detach subscriptions and manage them. To see the exact permissions that are required, expand the Required permissionssection:
Required permissions
The following permissions are required to detach subscriptions and manage them:
- Pull from a subscription:
pubsub.subscriptions.consume
- Create a subscription:
pubsub.subscriptions.create
- Delete a subscription:
pubsub.subscriptions.delete
- Get a subscription:
pubsub.subscriptions.get
- List a subscription:
pubsub.subscriptions.list
- Update a subscription:
pubsub.subscriptions.update
- Attach a subscription to a topic:
pubsub.topics.attachSubscription
- Get the IAM policy for a subscription:
pubsub.subscriptions.getIamPolicy
- Configure the IAM policy
for a subscription:
pubsub.subscriptions.setIamPolicy
You might also be able to get these permissions with custom roles or other predefined roles .
You can configure access control at the project level and at the individual resource level. You can create a subscription in one project and attach it to a topic located in a different project. Ensure that you have the required permissions for each project.
Detach a subscription from a topic
You can detach a subscription from a topic using the Google Cloud console, the Google Cloud CLI, the client library, or the Pub/Sub API.
Console
To detach a subscription, follow these steps:
-
In the Google Cloud console, go to the Topicspage.
-
Select the topic from which you want to detach a subscription.
-
In the Subscriptionstab, select the subscription to detach.
-
In the Subscription detailspage, click Detach.
-
In the dialog that appears, click Detachagain.
gcloud
-
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
-
To detach a subscription, use the
gcloud pubsub topics detach-subscription
command:gcloud pubsub topics detach-subscription SUBSCRIPTION_ID
If the request is successful, the command line displays a confirmation:
Detached subscription [ SUBSCRIPTION_ID ].
REST
To detach a subscription, use the projects.subscriptions.detach
method.
Request:
The request must be authenticated with an access token in the Authorization
header. To obtain an access token for the current
Application Default Credentials, use the gcloud auth application-default print-access-token
command.
POST https://pubsub.googleapis.com/v1/projects/ PROJECT_ID /subscriptions/ SUBSCRIPTION_ID :detach Authorization: Bearer ACCESS_TOKEN
Where:
- PROJECT_ID is your project ID.
- SUBSCRIPTION_ID is your subscription ID.
Response:
If the request is successful, the response is an empty JSON object.
C++
Before trying this sample, follow the C++ setup instructions in Quickstart: Using Client Libraries . For more information, see the Pub/Sub C++ API reference documentation .
C#
Before trying this sample, follow the C# setup instructions in Quickstart: Using Client Libraries . For more information, see the Pub/Sub C# API reference documentation .
Go
The following sample uses the major version of the Go Pub/Sub client library (v2). If you are still using the v1 library, see the migration guide to v2 . To see a list of v1 code samples, see the deprecated code samples .
Before trying this sample, follow the Go setup instructions in Quickstart: Using Client Libraries . For more information, see the Pub/Sub Go API reference documentation .
Java
Before trying this sample, follow the Java setup instructions in Quickstart: Using Client Libraries . For more information, see the Pub/Sub Java API reference documentation .
Node.js
Before trying this sample, follow the Node.js setup instructions in Quickstart: Using Client Libraries . For more information, see the Pub/Sub Node.js API reference documentation .
Node.ts
Before trying this sample, follow the Node.js setup instructions in Quickstart: Using Client Libraries . For more information, see the Pub/Sub Node.js API reference documentation .
PHP
Before trying this sample, follow the PHP setup instructions in Quickstart: Using Client Libraries . For more information, see the Pub/Sub PHP API reference documentation .
Python
Before trying this sample, follow the Python setup instructions in Quickstart: Using Client Libraries . For more information, see the Pub/Sub Python API reference documentation .
Ruby
The following sample uses Ruby Pub/Sub client library v3. If you are still using the v2 library, see the migration guide to v3 . To see a list of Ruby v2 code samples, see the deprecated code samples .
Before trying this sample, follow the Ruby setup instructions in Quickstart: Using Client Libraries . For more information, see the Pub/Sub Ruby API reference documentation .
The Pub/Sub service might take several minutes to finish detaching the subscription from the topic.
After the Pub/Sub service detaches the subscription from the topic, the Pub/Sub service deletes any messages that it retains for the subscription. You can't retrieve these messages from the subscription or reattach the subscription to a topic. To free up your Google Cloud project quota, delete the subscription .
If the subscription and the topic are in different Google Cloud projects, the Pub/Sub service adds an entry to the audit logs of both projects.
What's next
- Create or modify a subscription with
gcloud
commands . - Create or modify a subscription with REST APIs .