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](https://docs.cloud.google.com/iam/docs/roles-permissions/managedkafka.md.txt#managedkafka.consumerGroupEditor) (`roles/managedkafka.consumerGroupEditor`) IAM role on your project.


For more information about granting roles, see [Manage access to projects, folders, and organizations](https://docs.cloud.google.com/iam/docs/granting-changing-revoking-access.md.txt).


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](https://docs.cloud.google.com/iam/docs/creating-custom-roles.md.txt) or
other [predefined roles](https://docs.cloud.google.com/iam/docs/roles-overview.md.txt#predefined).

## 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](https://docs.cloud.google.com/logging/docs/view/logs-explorer-interface.md.txt).
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](https://console.cloud.google.com/managedkafka/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](https://console.cloud.google.com/?cloudshell=true)


   At the bottom of the Google Cloud console, a
   [Cloud Shell](https://docs.cloud.google.com/shell/docs/how-cloud-shell-works.md.txt)
   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`](https://docs.cloud.google.com/sdk/gcloud/reference/managed-kafka/consumer-groups/delete) command:

   <br />

   ```bash
   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:
   - <var translate="no">CONSUMER_GROUP_ID</var>: The ID or name of the consumer group.

   - <var translate="no">CLUSTER_ID</var>: The ID or name of the cluster.

   - <var translate="no">LOCATION</var>: The location of the cluster.

   <br />

### Go

```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

```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

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

# TODO(developer)
# project_id = "my-project-id"
# region = "us-central1"
# cluster_id = "my-cluster"
# consumer_group_id = "my-consumer-group"

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 as e:
    print(f"Failed to delete consumer group {consumer_group_id} with error: {e.message}")
```

<br />

<br />

## What's next?

- [View a consumer group](https://docs.cloud.google.com/managed-service-for-apache-kafka/docs/view-consumer-group.md.txt).
- [List all consumer groups of a cluster](https://docs.cloud.google.com/managed-service-for-apache-kafka/docs/list-consumer-groups.md.txt).
- [Edit a consumer group](https://docs.cloud.google.com/managed-service-for-apache-kafka/docs/edit-consumer-group.md.txt).

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