Creating and managing Lite subscriptions

This page explains how to create, view, and delete Lite subscriptions.

Lite subscriptions are zonal resources that subscribers use to read messages from Lite topics. When you create a Lite subscription, you attach it to a Lite topic. You can attach many Lite subscriptions to one Lite topic.

After creating a Lite subscription to a Lite topic, you can receive messages from the Lite subscription.

Creating Lite subscriptions

A Lite subscription must be in the same project and zone as the Lite topic that the Lite subscription is attached to. For a list of the available zones, see Pub/Sub Lite locations.

You can create Lite subscriptions with the Cloud Console, the gcloud command-line tool, or the Pub/Sub Lite API.

Console

  1. In the Cloud Console, go to the Lite Subscriptions page.

    Go to the Lite Subscriptions page

  2. Click Create Lite subscription.

  3. Enter a Lite subscription ID.

  4. Choose a Lite topic to receive messages from.

  5. Choose Deliver messages immediately or Deliver messages after stored.

  6. Click Create.

gcloud

To create a Lite subscription, use the gcloud beta pubsub lite-subscriptions create command:

gcloud beta pubsub lite-subscriptions create SUBSCRIPTION_ID \
  --zone=ZONE \
  --topic=TOPIC_ID \
  --delivery-requirement=DELIVERY_REQUIREMENT

Replace the following:

  • SUBSCRIPTION_ID: the ID of the Lite subscription

  • ZONE: the name of a zone that Pub/Sub Lite supports

  • TOPIC_ID: the ID of the Lite topic to attach to the Lite subscription

  • DELIVERY_REQUIREMENT: deliver-after-stored or deliver-immediately

If the request is successful, the command line displays a confirmation:

Created [SUBSCRIPTION_ID].

Protocol

To create a Lite subscription, send a POST request like the following:

POST https://REGION-pubsublite.googleapis.com/v1/admin/projects/PROJECT_NUMBER/locations/ZONE/subscriptions/SUBSCRIPTION_ID
Authorization: Bearer $(gcloud auth print-access-token)

Replace the following:

  • REGION: the region of the zone to store the Lite subscription in

  • PROJECT_NUMBER: the project number of the project to create the Lite subscription in

  • ZONE: the name of a zone that Pub/Sub Lite supports

  • SUBSCRIPTION_ID: the ID of the Lite subscription

Specify the following fields in the request body:

{
  "topic": projects/PROJECT_NUMBER/locations/ZONE/topics/TOPIC_ID,
  "deliveryConfig": {
    "deliveryRequirement": DELIVERY_REQUIREMENT,
   }
}

Replace DELIVERY_REQUIREMENT with deliver-after-stored or deliver-immediately.

If the request is successful, the response is the Lite subscription in JSON format:

{
  "deliveryConfig": {
    "deliveryRequirement": DELIVERY_REQUIREMENT,
   }
  "name": "projects/PROJECT_NUMBER/locations/ZONE/subscriptions/SUBSCRIPTION_ID",
  "topic": "projects/PROJECT_NUMBER/locations/ZONE/topics/TOPIC_ID",
}

Java

Before running this sample, follow the Java setup instructions in Pub/Sub Lite Client Libraries.

import com.google.cloud.pubsublite.AdminClient;
import com.google.cloud.pubsublite.AdminClientSettings;
import com.google.cloud.pubsublite.CloudRegion;
import com.google.cloud.pubsublite.CloudZone;
import com.google.cloud.pubsublite.ProjectNumber;
import com.google.cloud.pubsublite.SubscriptionName;
import com.google.cloud.pubsublite.SubscriptionPath;
import com.google.cloud.pubsublite.TopicName;
import com.google.cloud.pubsublite.TopicPath;
import com.google.cloud.pubsublite.proto.Subscription;
import com.google.cloud.pubsublite.proto.Subscription.DeliveryConfig;
import com.google.cloud.pubsublite.proto.Subscription.DeliveryConfig.DeliveryRequirement;

public class CreateSubscriptionExample {

  public static void main(String... args) throws Exception {
    // TODO(developer): Replace these variables before running the sample.
    String cloudRegion = "your-cloud-region";
    char zoneId = 'b';
    String topicId = "your-topic-id";
    String subscriptionId = "your-subscription-id";
    long projectNumber = Long.parseLong("123456789");

    createSubscriptionExample(cloudRegion, zoneId, projectNumber, topicId, subscriptionId);
  }

  public static void createSubscriptionExample(
      String cloudRegion, char zoneId, long projectNumber, String topicId, String subscriptionId)
      throws Exception {

    TopicPath topicPath =
        TopicPath.newBuilder()
            .setProject(ProjectNumber.of(projectNumber))
            .setLocation(CloudZone.of(CloudRegion.of(cloudRegion), zoneId))
            .setName(TopicName.of(topicId))
            .build();

    SubscriptionPath subscriptionPath =
        SubscriptionPath.newBuilder()
            .setLocation(CloudZone.of(CloudRegion.of(cloudRegion), zoneId))
            .setProject(ProjectNumber.of(projectNumber))
            .setName(SubscriptionName.of(subscriptionId))
            .build();

    Subscription subscription =
        Subscription.newBuilder()
            .setDeliveryConfig(
                // The server does not wait for a published message to be successfully
                // written to storage before delivering it to subscribers. As such, a
                // subscriber may receive a message for which the write to storage failed.
                // If the subscriber re-reads the offset of that message later on, there
                // may be a gap at that offset.
                DeliveryConfig.newBuilder()
                    .setDeliveryRequirement(DeliveryRequirement.DELIVER_IMMEDIATELY))
            .setName(subscriptionPath.toString())
            .setTopic(topicPath.toString())
            .build();

    AdminClientSettings adminClientSettings =
        AdminClientSettings.newBuilder().setRegion(CloudRegion.of(cloudRegion)).build();

    try (AdminClient adminClient = AdminClient.create(adminClientSettings)) {
      Subscription response = adminClient.createSubscription(subscription).get();
      System.out.println(response.getAllFields() + "created successfully.");
    }
  }
}

Lite subscriptions have a delivery requirement property. Lite subscriptions can deliver messages to subscribers immediately after the Pub/Sub Lite service receives the messages or only after the Pub/Sub Lite service stores the message in a partition.

If the delivery requirement is deliver-immediately and the Pub/Sub Lite service fails to write a message to storage, the Pub/Sub Lite service returns an error to the publisher but subscribers might still receive the message.

Updating Lite subscriptions

You can update Lite subscriptions with the Cloud Console, the gcloud command-line tool, or the Pub/Sub Lite API.

Console

  1. In the Cloud Console, go to the Lite Subscriptions page.

    Go to the Lite Subscriptions page

  2. Click the Lite subscription ID.

  3. In the Lite subscription details page, click Edit.

gcloud

To update a Lite subscription, use the gcloud beta pubsub lite-subscriptions update command:

gcloud beta pubsub lite-subscriptions update SUBSCRIPTION_ID \
  --zone=ZONE \
  --delivery-requirement=DELIVERY_REQUIREMENT

Replace the following:

  • SUBSCRIPTION_ID: the ID of the Lite subscription

  • ZONE: the name of the zone that the Lite subscription is in

  • DELIVERY_REQUIREMENT: deliver-after-stored or deliver-immediately

If the request is successful, the command line displays the Lite subscription:

deliveryConfig:
  deliveryRequirement: DELIVERY_REQUIREMENT
name: projects/PROJECT_NUMBER/locations/ZONE/subscriptions/SUBSCRIPTION_ID
topic: projects/PROJECT_NUMBER/locations/ZONE/topics/TOPIC_ID

Protocol

To update a Lite subscription, send a PATCH request like the following:

PATCH https://REGION-pubsublite.googleapis.com/v1/admin/projects/PROJECT_NUMBER/locations/ZONE/subscriptions/SUBSCRIPTION_ID?updateMask=deliveryConfig.deliveryRequirement
Authorization: Bearer $(gcloud auth print-access-token)

Replace the following:

  • REGION: the region of the zone to store the Lite subscription in

  • PROJECT_NUMBER: the project number of the project with the Lite subscription

  • ZONE: the name of the zone that the Lite subscription is in

  • SUBSCRIPTION_ID: the ID of the Lite subscription

Specify the following fields in the request body:

{
  "deliveryConfig": {
    "deliveryRequirement": DELIVERY_REQUIREMENT,
   }
}

Replace DELIVERY_REQUIREMENT with deliver-after-stored or deliver-immediately.

If the request is successful, the response is the Lite subscription in JSON format:

{
  "deliveryConfig": {
    "deliveryRequirement": DELIVERY_REQUIREMENT,
   }
  "name": "projects/PROJECT_NUMBER/locations/ZONE/subscriptions/SUBSCRIPTION_ID",
  "topic": "projects/PROJECT_NUMBER/locations/ZONE/topics/TOPIC_ID",
}

Java

Before running this sample, follow the Java setup instructions in Pub/Sub Lite Client Libraries.

import com.google.cloud.pubsublite.AdminClient;
import com.google.cloud.pubsublite.AdminClientSettings;
import com.google.cloud.pubsublite.CloudRegion;
import com.google.cloud.pubsublite.CloudZone;
import com.google.cloud.pubsublite.ProjectNumber;
import com.google.cloud.pubsublite.SubscriptionName;
import com.google.cloud.pubsublite.SubscriptionPath;
import com.google.cloud.pubsublite.proto.Subscription;
import com.google.cloud.pubsublite.proto.Subscription.DeliveryConfig;
import com.google.cloud.pubsublite.proto.Subscription.DeliveryConfig.DeliveryRequirement;
import com.google.protobuf.FieldMask;

public class UpdateSubscriptionExample {

  public static void main(String... args) throws Exception {
    // TODO(developer): Replace these variables before running the sample.
    String cloudRegion = "your-cloud-region";
    char zoneId = 'b';
    // Choose an existing subscription for the sample to work.
    String subscriptionId = "your-subscription-id";
    long projectNumber = Long.parseLong("123456789");

    updateSubscriptionExample(cloudRegion, zoneId, projectNumber, subscriptionId);
  }

  public static void updateSubscriptionExample(
      String cloudRegion, char zoneId, long projectNumber, String subscriptionId) throws Exception {
    SubscriptionPath subscriptionPath =
        SubscriptionPath.newBuilder()
            .setLocation(CloudZone.of(CloudRegion.of(cloudRegion), zoneId))
            .setProject(ProjectNumber.of(projectNumber))
            .setName(SubscriptionName.of(subscriptionId))
            .build();

    FieldMask fieldMask =
        FieldMask.newBuilder().addPaths("delivery_config.delivery_requirement").build();

    Subscription subscription =
        Subscription.newBuilder()
            .setDeliveryConfig(
                // DELIVER_AFTER_STORED ensures that the server won't deliver a published message
                // to subscribers until the message has been successfully written to storage.
                DeliveryConfig.newBuilder()
                    .setDeliveryRequirement(DeliveryRequirement.DELIVER_AFTER_STORED))
            .setName(subscriptionPath.toString())
            .build();

    AdminClientSettings adminClientSettings =
        AdminClientSettings.newBuilder().setRegion(CloudRegion.of(cloudRegion)).build();

    try (AdminClient adminClient = AdminClient.create(adminClientSettings)) {
      Subscription subscriptionBeforeUpdate = adminClient.getSubscription(subscriptionPath).get();
      System.out.println("Before update: " + subscriptionBeforeUpdate.getAllFields());

      Subscription subscriptionAfterUpdate =
          adminClient.updateSubscription(subscription, fieldMask).get();
      System.out.println("After update: " + subscriptionAfterUpdate.getAllFields());
    }
  }
}

Getting Lite subscription details

You can get details about a Lite subscription using the Cloud Console, the gcloud command-line tool, or the Pub/Sub Lite API.

Console

  1. In the Cloud Console, go to the Lite Subscriptions page.

    Go to the Lite Subscriptions page

  2. Click the Lite subscription ID.

gcloud

To get details about a Lite subscription, use the gcloud beta pubsub lite-subscriptions describe command:

gcloud beta pubsub lite-subscriptions describe SUBSCRIPTION_ID \
  --zone=ZONE

Replace the following:

  • SUBSCRIPTION_ID: the ID of the Lite subscription

  • ZONE: the name of the zone that the Lite subscription is in

If the request is successful, the command line displays the Lite topic:

deliveryConfig:
  deliveryRequirement: DELIVERY_REQUIREMENT
name: projects/PROJECT_NUMBER/locations/ZONE/subscriptions/SUBSCRIPTION_ID
topic: projects/PROJECT_NUMBER/locations/ZONE/topics/TOPIC_ID

Protocol

To get details about a Lite subscription, send a GET request like the following:

GET https://REGION-pubsublite.googleapis.com/v1/admin/projects/PROJECT_NUMBER/locations/ZONE/subscriptions/SUBSCRIPTION_ID
Authorization: Bearer $(gcloud auth print-access-token)

Replace the following:

  • REGION: the region of the zone to store the Lite subscription in

  • PROJECT_NUMBER: the project number of the project with the Lite subscription

  • ZONE: the name of the zone that the Lite subscription is in

  • SUBSCRIPTION_ID: the ID of the Lite subscription

If the request is successful, the response is the Lite topic in JSON format:

{
  "deliveryConfig": {
    "deliveryRequirement": DELIVERY_REQUIREMENT,
   }
  "name": "projects/PROJECT_NUMBER/locations/ZONE/subscriptions/SUBSCRIPTION_ID",
  "topic": "projects/PROJECT_NUMBER/locations/ZONE/topics/TOPIC_ID",
}

Java

Before running this sample, follow the Java setup instructions in Pub/Sub Lite Client Libraries.

import com.google.cloud.pubsublite.AdminClient;
import com.google.cloud.pubsublite.AdminClientSettings;
import com.google.cloud.pubsublite.CloudRegion;
import com.google.cloud.pubsublite.CloudZone;
import com.google.cloud.pubsublite.ProjectNumber;
import com.google.cloud.pubsublite.SubscriptionName;
import com.google.cloud.pubsublite.SubscriptionPath;
import com.google.cloud.pubsublite.proto.Subscription;

public class GetSubscriptionExample {

  public static void main(String... args) throws Exception {
    // TODO(developer): Replace these variables before running the sample.
    String cloudRegion = "your-cloud-region";
    char zoneId = 'b';
    // Choose an existing subscription.
    String subscriptionId = "your-subscription-id";
    long projectNumber = Long.parseLong("123456789");

    getSubscriptionExample(cloudRegion, zoneId, projectNumber, subscriptionId);
  }

  public static void getSubscriptionExample(
      String cloudRegion, char zoneId, long projectNumber, String subscriptionId) throws Exception {

    SubscriptionPath subscriptionPath =
        SubscriptionPath.newBuilder()
            .setLocation(CloudZone.of(CloudRegion.of(cloudRegion), zoneId))
            .setProject(ProjectNumber.of(projectNumber))
            .setName(SubscriptionName.of(subscriptionId))
            .build();

    AdminClientSettings adminClientSettings =
        AdminClientSettings.newBuilder().setRegion(CloudRegion.of(cloudRegion)).build();

    try (AdminClient adminClient = AdminClient.create(adminClientSettings)) {
      Subscription subscription = adminClient.getSubscription(subscriptionPath).get();
      System.out.println("Subscription: " + subscription.getAllFields());
    }
  }
}

Listing Lite subscriptions

You can list the Lite subscriptions in a project or the Lite subscriptions to a Lite topic.

Listing Lite subscriptions in a project

You can list Lite subscriptions in a project using the Cloud Console, the gcloud command-line tool, or the Pub/Sub Lite API.

Console

To view a list of the Lite subscriptions in a project, go to the Lite Subscriptions page.

gcloud

To list the Lite subscriptions in a project, use the gcloud beta pubsub lite-subscriptions list command:

gcloud beta pubsub lite-subscriptions list \
  --zone=ZONE

Replace ZONE with the name of the zone that the Lite subscriptions are in.

If the request is successful, the command line displays the Lite subscriptions:

---
deliveryConfig:
  deliveryRequirement: DELIVERY_REQUIREMENT
name: projects/PROJECT_NUMBER/locations/ZONE/subscriptions/SUBSCRIPTION_ID
topic: projects/PROJECT_NUMBER/locations/ZONE/topics/TOPIC_ID
---
deliveryConfig:
  deliveryRequirement: DELIVERY_REQUIREMENT
name: projects/PROJECT_NUMBER/locations/ZONE/subscriptions/SUBSCRIPTION_ID
topic: projects/PROJECT_NUMBER/locations/ZONE/topics/TOPIC_ID

Protocol

To list the Lite subscriptions in a project, send a GET request like the following:

GET https://REGION-pubsublite.googleapis.com/v1/admin/projects/PROJECT_NUMBER/subscriptions
Authorization: Bearer $(gcloud auth print-access-token)

Replace the following:

  • REGION: the region of the zone to store the Lite subscription in

  • PROJECT_NUMBER: the project number of the project with the Lite subscription

If the request is successful, the response is the Lite topic in JSON format:

{
  "subscriptions": [
      {
          "name": "projects/PROJECT_NUMBER/locations/ZONE/subscriptions/SUBSCRIPTION_ID"
      },
      {
          "name": "projects/PROJECT_NUMBER/locations/ZONE/subscriptions/SUBSCRIPTION_ID"
      }
  ]
}

Java

Before running this sample, follow the Java setup instructions in Pub/Sub Lite Client Libraries.

import com.google.cloud.pubsublite.AdminClient;
import com.google.cloud.pubsublite.AdminClientSettings;
import com.google.cloud.pubsublite.CloudRegion;
import com.google.cloud.pubsublite.CloudZone;
import com.google.cloud.pubsublite.LocationPath;
import com.google.cloud.pubsublite.ProjectNumber;
import com.google.cloud.pubsublite.proto.Subscription;
import java.util.List;

public class ListSubscriptionsInProjectExample {

  public static void main(String... args) throws Exception {
    // TODO(developer): Replace these variables before running the sample.
    String cloudRegion = "your-cloud-region";
    char zoneId = 'b';
    long projectNumber = Long.parseLong("123456789");

    listSubscriptionsInProjectExample(cloudRegion, zoneId, projectNumber);
  }

  public static void listSubscriptionsInProjectExample(
      String cloudRegion, char zoneId, long projectNumber) throws Exception {

    AdminClientSettings adminClientSettings =
        AdminClientSettings.newBuilder().setRegion(CloudRegion.of(cloudRegion)).build();

    LocationPath locationPath =
        LocationPath.newBuilder()
            .setProject(ProjectNumber.of(projectNumber))
            .setLocation(CloudZone.of(CloudRegion.of(cloudRegion), zoneId))
            .build();

    try (AdminClient adminClient = AdminClient.create(adminClientSettings)) {
      List<Subscription> subscriptions = adminClient.listSubscriptions(locationPath).get();
      for (Subscription subscription : subscriptions) {
        System.out.println(subscription.getAllFields());
      }
      System.out.println(subscriptions.size() + " subscription(s) listed.");
    }
  }
}

Listing Lite subscriptions to a Lite topic

You can list Lite subscriptions to a Lite topic using the Cloud Console, the gcloud command-line tool, or the Pub/Sub Lite API.

Console

  1. In the Cloud Console, go to the Lite Topics page.

    Go to the Lite Topics page

  2. Select a Lite topic ID.

  3. In the Lite topics details page, the Lite subscriptions section includes a list of Lite subscriptions to the Lite topic.

gcloud

To list the Lite subscriptions to a topic, use the gcloud beta pubsub lite-topics list-subscriptions command:

gcloud beta pubsub lite-topics list-subscriptions TOPIC_ID \
  --zone=ZONE

Replace the following:

  • TOPIC_ID: the ID of the Lite topic that the Lite subscriptions are attached to

  • ZONE: the name of the zone that the Lite subscription is in

If the request is successful, the command line displays the Lite subscriptions:

---
deliveryConfig:
  deliveryRequirement: DELIVERY_REQUIREMENT
name: projects/PROJECT_NUMBER/locations/ZONE/subscriptions/SUBSCRIPTION_ID
topic: projects/PROJECT_NUMBER/locations/ZONE/topics/TOPIC_ID
---
deliveryConfig:
  deliveryRequirement: DELIVERY_REQUIREMENT
name: projects/PROJECT_NUMBER/locations/ZONE/subscriptions/SUBSCRIPTION_ID
topic: projects/PROJECT_NUMBER/locations/ZONE/topics/TOPIC_ID

Protocol

To list the Lite subscriptions to a Lite topic, send a GET request like the following:

GET https://REGION-pubsublite.googleapis.com/v1/admin/projects/PROJECT_NUMBER/lite-topics/TOPIC_ID/subscriptions
Authorization: Bearer $(gcloud auth print-access-token)

Replace the following:

  • REGION: the region of the zone to store the Lite subscription in

  • PROJECT_NUMBER: the project number of the project with the Lite subscription

  • ZONE: the name of the zone that the Lite subscription is in

  • TOPIC_ID: the ID of the Lite topic that the Lite subscriptions are attached to

If the request is successful, the response is a list of Lite subscriptions in JSON format:

{
  "subscriptions": [
      {
          "name": "projects/PROJECT_NUMBER/locations/ZONE/subscriptions/SUBSCRIPTION_ID"
      },
      {
          "name": "projects/PROJECT_NUMBER/locations/ZONE/subscriptions/SUBSCRIPTION_ID"
      }
  ]
}

Java

Before running this sample, follow the Java setup instructions in Pub/Sub Lite Client Libraries.

import com.google.cloud.pubsublite.AdminClient;
import com.google.cloud.pubsublite.AdminClientSettings;
import com.google.cloud.pubsublite.CloudRegion;
import com.google.cloud.pubsublite.CloudZone;
import com.google.cloud.pubsublite.ProjectNumber;
import com.google.cloud.pubsublite.SubscriptionPath;
import com.google.cloud.pubsublite.TopicName;
import com.google.cloud.pubsublite.TopicPath;
import java.util.List;

public class ListSubscriptionsInTopicExample {

  public static void main(String... args) throws Exception {
    // TODO(developer): Replace these variables before running the sample.
    String cloudRegion = "your-cloud-region";
    char zoneId = 'b';
    long projectNumber = Long.parseLong("123456789");
    String topicId = "your-topic-id";

    listSubscriptionsInTopicExample(cloudRegion, zoneId, projectNumber, topicId);
  }

  public static void listSubscriptionsInTopicExample(
      String cloudRegion, char zoneId, long projectNumber, String topicId) throws Exception {

    TopicPath topicPath =
        TopicPath.newBuilder()
            .setProject(ProjectNumber.of(projectNumber))
            .setLocation(CloudZone.of(CloudRegion.of(cloudRegion), zoneId))
            .setName(TopicName.of(topicId))
            .build();

    AdminClientSettings adminClientSettings =
        AdminClientSettings.newBuilder().setRegion(CloudRegion.of(cloudRegion)).build();

    try (AdminClient adminClient = AdminClient.create(adminClientSettings)) {
      List<SubscriptionPath> subscriptionPaths =
          adminClient.listTopicSubscriptions(topicPath).get();
      for (SubscriptionPath subscription : subscriptionPaths) {
        System.out.println(subscription.toString());
      }
      System.out.println(subscriptionPaths.size() + " subscription(s) listed.");
    }
  }
}

Deleting Lite subscriptions

You can delete Lite subscriptions with the Cloud Console, the gcloud command-line tool, or the Pub/Sub Lite API.

Console

  1. In the Cloud Console, go to the Lite Subscriptions page.

    Go to the Lite Subscriptions page

  2. Click the Lite subscription ID.

  3. In the Lite subscription details page, click Delete.

  4. In the dialog that appears, click Delete to confirm that you want to delete the Lite subscription.

gcloud

To delete a Lite subscription, use the gcloud beta pubsub lite-subscriptions delete command:

  1. Run the delete command:

    gcloud beta pubsub lite-subscriptions delete SUBSCRIPTION_ID \
     --zone=ZONE
    

    Replace the following:

    • SUBSCRIPTION_ID: the ID of the Lite subscription

    • ZONE: the name of the zone that the Lite subscription is in

  2. To confirm, type Y.

If the request is successful, the command line displays a confirmation:

Deleted subscription [SUBSCRIPTION_ID].

Protocol

To delete a Lite topic, send a DELETE request like the following:

DELETE https://REGION-pubsublite.googleapis.com/v1/admin/projects/PROJECT_NUMBER/locations/ZONE/subscriptions/SUBSCRIPTION_ID
Authorization: Bearer $(gcloud auth print-access-token)

Replace the following:

  • REGION: the region of the zone to store the Lite subscription in

  • PROJECT_NUMBER: the project number of the project with the Lite subscription

  • ZONE: the name of the zone that the Lite subscription is in

  • SUBSCRIPTION_ID: the ID of the Lite subscription

If the request is successful, the response is an empty JSON object.

Java

Before running this sample, follow the Java setup instructions in Pub/Sub Lite Client Libraries.

import com.google.cloud.pubsublite.AdminClient;
import com.google.cloud.pubsublite.AdminClientSettings;
import com.google.cloud.pubsublite.CloudRegion;
import com.google.cloud.pubsublite.CloudZone;
import com.google.cloud.pubsublite.ProjectNumber;
import com.google.cloud.pubsublite.SubscriptionName;
import com.google.cloud.pubsublite.SubscriptionPath;

public class DeleteSubscriptionExample {

  public static void main(String... args) throws Exception {
    // TODO(developer): Replace these variables before running the sample.
    String cloudRegion = "your-cloud-region";
    char zoneId = 'b';
    // Choose an existing subscription.
    String subscriptionId = "your-subscription-id";
    long projectNumber = Long.parseLong("123456789");

    deleteSubscriptionExample(cloudRegion, zoneId, projectNumber, subscriptionId);
  }

  public static void deleteSubscriptionExample(
      String cloudRegion, char zoneId, long projectNumber, String subscriptionId) throws Exception {

    SubscriptionPath subscriptionPath =
        SubscriptionPath.newBuilder()
            .setLocation(CloudZone.of(CloudRegion.of(cloudRegion), zoneId))
            .setProject(ProjectNumber.of(projectNumber))
            .setName(SubscriptionName.of(subscriptionId))
            .build();

    AdminClientSettings adminClientSettings =
        AdminClientSettings.newBuilder().setRegion(CloudRegion.of(cloudRegion)).build();

    try (AdminClient adminClient = AdminClient.create(adminClientSettings)) {
      adminClient.deleteSubscription(subscriptionPath).get();
      System.out.println(subscriptionPath.toString() + " deleted successfully.");
    }
  }
}