This document describes how to delete a Pub/Sub topic. To delete a topic you can use the Google Cloud console, the Google CLI, the client library, or the Pub/Sub API.
Before you begin
- Learn about topics and the publish messages workflow.
- Create a topic .
Required roles and permissions
To get the permissions that
you need to delete topics 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 delete topics and manage them. To see the exact permissions that are required, expand the Required permissionssection:
Required permissions
The following permissions are required to delete topics and manage them:
- Create a topic:
pubsub.topics.create
- Delete a topic:
pubsub.topics.delete
- Detach a subscription from a topic:
pubsub.topics.detachSubscription
- Get a topic:
pubsub.topics.get
- List a topic:
pubsub.topics.list
- Publish to a topic:
pubsub.topics.publish
- Update a topic:
pubsub.topics.update
- Get the IAM policy for a topic:
pubsub.topics.getIamPolicy
- Configure the IAM policy
for a topic:
pubsub.topics.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.
Delete a topic
When you delete a topic, its subscriptions are not deleted. The
message backlog from the subscription is available for subscribers. After a topic is
deleted, its subscriptions have the topic name _deleted-topic_
. If you try
to create a topic with the same name as the one you had just deleted, expect
an error for a brief period.
Console
-
In the Google Cloud console, go to the Pub/Sub Topics page.
-
Select a topic and click More actions .
-
Click Delete .
The Delete topic window appears.
-
Enter
delete
and then click Delete .
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 delete a topic, use the
gcloud pubsub topics delete
command:gcloud pubsub topics delete TOPIC_ID
REST
To delete a topic, use the projects.topics.delete
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: gcloud auth application-default print-access-token
.
DELETE https://pubsub.googleapis.com/v1/projects/ PROJECT_ID /topics/ TOPIC_ID Authorization: Bearer ACCESS_TOKEN
Where:
- PROJECT_ID is your project ID.
- TOPIC_ID is your topic 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 .
What's next
-
Choose the type of subscription for your topic.
-
Create a subscription for your topic: pull , push , or BigQuery .
-
Create or modify a topic with gcloud CLI .
-
Create or modify a topic with REST APIs .