Delete a Google Cloud Managed Service for Apache Kafka consumer group

Deleting a consumer group removes a consumer group from your Managed Service for Apache Kafka cluster.

To delete a consumer group, you can use the Google Cloud console, the Google Cloud CLI, the client libraries, the Managed Kafka API, or the open source Apache Kafka APIs.

Required roles and permissions to delete a consumer group

To get the permissions that you need to delete your consumer groups, ask your administrator to grant you the Managed Kafka Consumer Group Editor (roles/managedkafka.consumerGroupEditor) IAM role on your project. For more information about granting roles, see Manage access to projects, folders, and organizations.

This predefined role contains the permissions required to delete your consumer groups. To see the exact permissions that are required, expand the Required permissions section:

Required permissions

The following permissions are required to delete your consumer groups:

  • Delete consumer groups: managedkafka.consumerGroups.delete

You might also be able to get these permissions with custom roles or other predefined roles.

For more information about the Managed Kafka Consumer Group Editor role, see Managed Service for Apache Kafka predefined roles.

Delete a consumer group

When you delete a consumer group, the consumer offsets it stores are permanently lost. You are also unable to see the logs and metrics for the consumer groups in the console. However, the metrics and logs associated with the consumer group are retained and accessible using Logs Explorer. Deleting a consumer group also does not delete the messages it has consumed. The messages are still available in the topics with which they were originally associated.

To delete a consumer group, follow these steps:

Console

  1. In the Google Cloud console, go to the Cluster page.

    Go to Clusters

  2. From the list of clusters, click the cluster to which the consumer group that you want to delete belongs.

    The Cluster details page opens.

  3. Click the consumer group that you want to delete.
  4. In the Consumer group details page, click Delete.
  5. Confirm the operation.

gcloud

  1. In the Google Cloud console, activate Cloud Shell.

    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.

  2. Run the gcloud managed-kafka consumer-groups delete command:

    gcloud managed-kafka consumer-groups delete CONSUMER_GROUP_ID \
        --cluster=CLUSTER_ID \
        --location=LOCATION

    This command permanently removes a consumer group from your Managed Service for Apache Kafka cluster.

    Replace the following:

    • CONSUMER_GROUP_ID: The ID or name of the consumer group.

    • CLUSTER_ID: The ID or name of the cluster.

    • LOCATION: The location of the cluster.

Go

import (
	"context"
	"fmt"
	"io"

	"cloud.google.com/go/managedkafka/apiv1/managedkafkapb"
	"google.golang.org/api/option"

	managedkafka "cloud.google.com/go/managedkafka/apiv1"
)

func deleteConsumerGroup(w io.Writer, projectID, region, clusterID, consumerGroupID string, opts ...option.ClientOption) error {
	// projectID := "my-project-id"
	// region := "us-central1"
	// clusterID := "my-cluster"
	// consumerGroupID := "my-consumer-group"
	ctx := context.Background()
	client, err := managedkafka.NewClient(ctx, opts...)
	if err != nil {
		return fmt.Errorf("managedkafka.NewClient got err: %w", err)
	}
	defer client.Close()

	clusterPath := fmt.Sprintf("projects/%s/locations/%s/clusters/%s", projectID, region, clusterID)
	consumerGroupPath := fmt.Sprintf("%s/consumerGroups/%s", clusterPath, consumerGroupID)
	req := &managedkafkapb.DeleteConsumerGroupRequest{
		Name: consumerGroupPath,
	}
	if err := client.DeleteConsumerGroup(ctx, req); err != nil {
		return fmt.Errorf("client.DeleteConsumerGroup got err: %w", err)
	}
	fmt.Fprint(w, "Deleted consumer group\n")
	return nil
}

Java

import com.google.api.gax.rpc.ApiException;
import com.google.cloud.managedkafka.v1.ConsumerGroupName;
import com.google.cloud.managedkafka.v1.ManagedKafkaClient;
import java.io.IOException;

public class DeleteConsumerGroup {

  public static void main(String[] args) throws Exception {
    // TODO(developer): Replace these variables before running the example.
    String projectId = "my-project-id";
    String region = "my-region"; // e.g. us-east1
    String clusterId = "my-cluster";
    String consumerGroupId = "my-consumer-group";
    deleteConsumerGroup(projectId, region, clusterId, consumerGroupId);
  }

  public static void deleteConsumerGroup(
      String projectId, String region, String clusterId, String consumerGroupId) throws Exception {
    try (ManagedKafkaClient managedKafkaClient = ManagedKafkaClient.create()) {
      // This operation is being handled synchronously.
      managedKafkaClient.deleteConsumerGroup(
          ConsumerGroupName.of(projectId, region, clusterId, consumerGroupId));
      System.out.println("Deleted consumer group");
    } catch (IOException | ApiException e) {
      System.err.printf("managedKafkaClient.getConsumerGroup got err: %s", e.getMessage());
    }
  }
}

Python

from google.api_core.exceptions import NotFound
from google.cloud import managedkafka_v1


def delete_consumer_group(
    project_id: str,
    region: str,
    cluster_id: str,
    consumer_group_id: str,
) -> None:
    """
    Delete a Kafka consumer group.

    Args:
        project_id: Google Cloud project ID.
        region: Cloud region.
        cluster_id: ID of the Kafka cluster.
        consumer_group_id: ID of the Kafka consumer group.

    Raises:
        This method will raise the exception if the consumer group is not found.
    """

    client = managedkafka_v1.ManagedKafkaClient()

    consumer_group_path = client.consumer_group_path(
        project_id, region, cluster_id, consumer_group_id
    )
    request = managedkafka_v1.DeleteConsumerGroupRequest(
        name=consumer_group_path,
    )

    try:
        client.delete_consumer_group(request=request)
        print("Deleted consumer group")
    except NotFound:
        print(f"Consumer group {consumer_group_path} not found")

What's next?

Apache Kafka® is a registered trademark of The Apache Software Foundation or its affiliates in the United States and/or other countries.