Delete Lite topic
Stay organized with collections
Save and categorize content based on your preferences.
Delete a Lite topic.
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[],[],null,["# Delete Lite topic\n\nDelete a Lite topic.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Create and manage Lite topics](/pubsub/lite/docs/topics)\n\nCode sample\n-----------\n\n### Go\n\n\nTo authenticate to Pub/Sub Lite, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import (\n \t\"context\"\n \t\"fmt\"\n \t\"io\"\n\n \t\"cloud.google.com/go/pubsublite\"\n )\n\n func deleteTopic(w io.Writer, projectID, region, location, topicID string) error {\n \t// projectID := \"my-project-id\"\n \t// region := \"us-central1\"\n \t// NOTE: location can be either a region (\"us-central1\") or a zone (\"us-central1-a\")\n \t// For a list of valid locations, see https://cloud.google.com/pubsub/lite/docs/locations.\n \t// location := \"us-central1\"\n \t// topicID := \"my-topic\"\n \tctx := context.Background()\n \tclient, err := pubsublite.NewAdminClient(ctx, region)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"pubsublite.NewAdminClient: %w\", err)\n \t}\n \tdefer client.Close()\n\n \ttopicPath := fmt.Sprintf(\"projects/%s/locations/%s/topics/%s\", projectID, location, topicID)\n \terr = client.DeleteTopic(ctx, topicPath)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"client.DeleteTopic got err: %w\", err)\n \t}\n \tfmt.Fprint(w, \"Deleted topic\\n\")\n \treturn nil\n }\n\n### Java\n\n\nTo authenticate to Pub/Sub Lite, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import com.google.api.gax.rpc.https://cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.rpc.NotFoundException.html;\n import com.google.cloud.pubsublite.https://cloud.google.com/java/docs/reference/google-cloud-pubsublite/latest/com.google.cloud.pubsublite.AdminClient.html;\n import com.google.cloud.pubsublite.https://cloud.google.com/java/docs/reference/google-cloud-pubsublite/latest/com.google.cloud.pubsublite.AdminClientSettings.html;\n import com.google.cloud.pubsublite.https://cloud.google.com/java/docs/reference/google-cloud-pubsublite/latest/com.google.cloud.pubsublite.CloudRegion.html;\n import com.google.cloud.pubsublite.https://cloud.google.com/java/docs/reference/google-cloud-pubsublite/latest/com.google.cloud.pubsublite.CloudRegionOrZone.html;\n import com.google.cloud.pubsublite.https://cloud.google.com/java/docs/reference/google-cloud-pubsublite/latest/com.google.cloud.pubsublite.CloudZone.html;\n import com.google.cloud.pubsublite.https://cloud.google.com/java/docs/reference/google-cloud-pubsublite/latest/com.google.cloud.pubsublite.ProjectNumber.html;\n import com.google.cloud.pubsublite.https://cloud.google.com/java/docs/reference/google-cloud-pubsublite/latest/com.google.cloud.pubsublite.proto.TopicName.html;\n import com.google.cloud.pubsublite.https://cloud.google.com/java/docs/reference/google-cloud-pubsublite/latest/com.google.cloud.pubsublite.TopicPath.html;\n import java.util.concurrent.ExecutionException;\n\n public class DeleteTopicExample {\n\n public static void main(String... args) throws Exception {\n // TODO(developer): Replace these variables before running the sample.\n String cloudRegion = \"your-cloud-region\";\n char zoneId = 'b';\n // Choose an existing topic.\n String topicId = \"your-topic-id\";\n long projectNumber = Long.parseLong(\"123456789\");\n // True if using a regional location. False if using a zonal location.\n // https://cloud.google.com/pubsub/lite/docs/topics\n boolean regional = false;\n\n deleteTopicExample(cloudRegion, zoneId, projectNumber, topicId, regional);\n }\n\n public static void deleteTopicExample(\n String cloudRegion, char zoneId, long projectNumber, String topicId, boolean regional)\n throws Exception {\n\n https://cloud.google.com/java/docs/reference/google-cloud-pubsublite/latest/com.google.cloud.pubsublite.CloudRegionOrZone.html location;\n if (regional) {\n location = https://cloud.google.com/java/docs/reference/google-cloud-pubsublite/latest/com.google.cloud.pubsublite.CloudRegionOrZone.html.of(https://cloud.google.com/java/docs/reference/google-cloud-pubsublite/latest/com.google.cloud.pubsublite.CloudRegion.html.of(cloudRegion));\n } else {\n location = https://cloud.google.com/java/docs/reference/google-cloud-pubsublite/latest/com.google.cloud.pubsublite.CloudRegionOrZone.html.of(https://cloud.google.com/java/docs/reference/google-cloud-pubsublite/latest/com.google.cloud.pubsublite.CloudZone.html.of(https://cloud.google.com/java/docs/reference/google-cloud-pubsublite/latest/com.google.cloud.pubsublite.CloudRegion.html.of(cloudRegion), zoneId));\n }\n\n https://cloud.google.com/java/docs/reference/google-cloud-pubsublite/latest/com.google.cloud.pubsublite.TopicPath.html topicPath =\n https://cloud.google.com/java/docs/reference/google-cloud-pubsublite/latest/com.google.cloud.pubsublite.TopicPath.html.newBuilder()\n .setProject(https://cloud.google.com/java/docs/reference/google-cloud-pubsublite/latest/com.google.cloud.pubsublite.ProjectNumber.html.of(projectNumber))\n .setLocation(location)\n .setName(https://cloud.google.com/java/docs/reference/google-cloud-pubsublite/latest/com.google.cloud.pubsublite.proto.TopicName.html.of(topicId))\n .build();\n\n https://cloud.google.com/java/docs/reference/google-cloud-pubsublite/latest/com.google.cloud.pubsublite.AdminClientSettings.html adminClientSettings =\n https://cloud.google.com/java/docs/reference/google-cloud-pubsublite/latest/com.google.cloud.pubsublite.AdminClientSettings.html.newBuilder().setRegion(https://cloud.google.com/java/docs/reference/google-cloud-pubsublite/latest/com.google.cloud.pubsublite.CloudRegion.html.of(cloudRegion)).build();\n\n try (https://cloud.google.com/java/docs/reference/google-cloud-pubsublite/latest/com.google.cloud.pubsublite.AdminClient.html adminClient = https://cloud.google.com/java/docs/reference/google-cloud-pubsublite/latest/com.google.cloud.pubsublite.AdminClient.html.create(adminClientSettings)) {\n adminClient.deleteTopic(topicPath).get();\n if (regional) {\n System.out.println(https://cloud.google.com/java/docs/reference/pubsublite-spark-sql-streaming/latest/com.google.cloud.pubsublite.spark.PslWriteDataSourceOptions.html#com_google_cloud_pubsublite_spark_PslWriteDataSourceOptions_topicPath__.https://cloud.google.com/java/docs/reference/google-cloud-pubsublite/latest/com.google.cloud.pubsublite.TopicPath.html#com_google_cloud_pubsublite_TopicPath_toString__() + \" (regional topic) deleted successfully.\");\n } else {\n System.out.println(https://cloud.google.com/java/docs/reference/pubsublite-spark-sql-streaming/latest/com.google.cloud.pubsublite.spark.PslWriteDataSourceOptions.html#com_google_cloud_pubsublite_spark_PslWriteDataSourceOptions_topicPath__.https://cloud.google.com/java/docs/reference/google-cloud-pubsublite/latest/com.google.cloud.pubsublite.TopicPath.html#com_google_cloud_pubsublite_TopicPath_toString__() + \" (zonal topic) deleted successfully.\");\n }\n } catch (ExecutionException e) {\n try {\n throw e.getCause();\n } catch (https://cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.rpc.NotFoundException.html notFound) {\n System.out.println(\"This topic is not found.\");\n } catch (Throwable throwable) {\n throwable.printStackTrace();\n }\n }\n }\n\n### Python\n\n\nTo authenticate to Pub/Sub Lite, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n from google.api_core.exceptions import NotFound\n from google.cloud.pubsublite import https://cloud.google.com/python/docs/reference/pubsublite/latest/google.cloud.pubsublite.admin_client.AdminClient.html\n from google.cloud.pubsublite.types import https://cloud.google.com/python/docs/reference/pubsublite/latest/google.cloud.pubsublite.types.CloudRegion.html, https://cloud.google.com/python/docs/reference/pubsublite/latest/google.cloud.pubsublite.types.CloudZone.html, https://cloud.google.com/python/docs/reference/pubsublite/latest/google.cloud.pubsublite.types.TopicPath.html\n\n # TODO(developer):\n # project_number = 1122334455\n # cloud_region = \"us-central1\"\n # zone_id = \"a\"\n # topic_id = \"your-topic-id\"\n # regional = True\n\n cloud_region = CloudRegion(cloud_region)\n topic_path = None\n if regional:\n # A regional topic.\n topic_path = TopicPath(project_number, cloud_region, topic_id)\n else:\n # A zonal topic\n topic_path = TopicPath(\n project_number, CloudZone(cloud_region, zone_id), topic_id\n )\n\n client = AdminClient(cloud_region)\n try:\n client.https://cloud.google.com/python/docs/reference/pubsublite/latest/google.cloud.pubsublite.admin_client.AdminClient.html#google_cloud_pubsublite_admin_client_AdminClient_delete_topic(topic_path)\n if regional:\n print(f\"{topic_path} (regional topic) deleted successfully.\")\n else:\n print(f\"{topic_path} (zonal topic) deleted successfully.\")\n except NotFound:\n print(f\"{topic_path} not found.\")\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=pubsublite)."]]