[Yandex Cloud documentation](../../index.md) > [Yandex Managed Service for Apache Kafka®](../index.md) > [Step-by-step guides](index.md) > Managing topics

# Managing Apache Kafka® topics

A Managed Service for Apache Kafka® cluster allows you to manage topics and partitions in two ways, either separately or together:

* Using the [native Yandex Cloud interfaces](#yandex-cloud), such as the CLI, API, or management console. Choose this option if you want to manage topics using Managed Service for Apache Kafka® features.

    You can do the following with Managed Service for Apache Kafka® topics:

    * [Create a topic](#create-topic).
    * [Update topic settings](#update-topic).
    * [Get a list of topics in a cluster](#list-topics).
    * [Get detailed information about a topic](#get-topic).
    * [Import a topic to Terraform](#import-topic).
    * [Transfer information about created topics to the Terraform state file](#move-info-topic).
    * [Delete a topic](#delete-topic).

* Using the [Apache Kafka® Admin API](#admin-api). Select this option to use your own solutions to manage topics and partitions.

## Managing topics via Yandex Cloud interfaces {#yandex-cloud}

### Creating a topic {#create-topic}

Prior to creating a topic, calculate the [minimum storage size](../concepts/storage.md#minimal-storage-size).

{% list tabs group=instructions %}

- Management console {#console}

  1. In the [management console](https://console.yandex.cloud), navigate to the relevant folder.
  1. Navigate to **Managed Service for&nbsp;Kafka**.
  1. Click the cluster name and navigate to the **Topics** tab.
  1. Click **Create topic**.
  1. Under **Basic parameters**, set the basic topic properties:
     * Topic name, which must be unique within the Apache Kafka® cluster.

       {% note info %}
       
       Use the [Apache Kafka® Admin API](cluster-topics.md#admin-api) if you need to create a topic that starts with `_`. You cannot create such a topic using the Yandex Cloud interfaces.
       
       {% endnote %}

     * Number of topic partitions.
     * Replication factor. This value should not exceed the number of brokers in the cluster. The minimum value is `1`. The maximum value is `3`. The default value is:
       * For a cluster with one or two brokers: `1`.
       * For a cluster with three or more brokers: `3`.
  1. Under **Topic settings**, specify the [topic settings](../concepts/settings-list.md#topic-settings).
  1. Click **Create**.

- CLI {#cli}

  If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../cli/quickstart.md#install).

  The folder used by default is the one specified when [creating](../../cli/operations/profile/profile-create.md) the CLI profile. To change the default folder, use the `yc config set folder-id <folder_ID>` command. You can also specify a different folder for any command using `--folder-name` or `--folder-id`. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.

  To create a topic:
  1. See the description of the CLI command for creating topics:

     ```bash
     yc managed-kafka topic create --help
     ```

  1. Create a topic:

     ```bash
     yc managed-kafka topic create <topic_name> \
       --cluster-name <cluster_name> \
       --partitions <number_of_partitions> \
       --replication-factor <replication_factor>
     ```

     Specify the [topic settings](../concepts/settings-list.md#topic-settings) here, if required.

     {% note info %}
     
     Use the [Apache Kafka® Admin API](cluster-topics.md#admin-api) if you need to create a topic that starts with `_`. You cannot create such a topic using the Yandex Cloud interfaces.
     
     {% endnote %}

- Terraform {#tf}

  1. Open the current Terraform configuration file describing your infrastructure.

     For information about creating this file, see [Creating a cluster Apache Kafka®](cluster-create.md).
  1. Add the `yandex_mdb_kafka_topic` resource and specify the [topic settings](../concepts/settings-list.md#topic-settings) in the `topic_config`, if required:

     ```hcl
     resource "yandex_mdb_kafka_topic" "<topic_name>" {
       cluster_id         = "<cluster_ID>"
       name               = "<topic_name>"
       partitions         = <number_of_partitions>
       replication_factor = <replication_factor>
       topic_config {
         compression_type = "<compression_type>"
         flush_messages   = <maximum_number_of_messages_in_memory>
         ...
       }
     }
     ```

     {% note info %}
     
     Use the [Apache Kafka® Admin API](cluster-topics.md#admin-api) if you need to create a topic that starts with `_`. You cannot create such a topic using the Yandex Cloud interfaces.
     
     {% endnote %}

  1. Make sure the settings are correct.

     1. In the command line, navigate to the directory that contains the current Terraform configuration files defining the infrastructure.
     1. Run this command:
     
        ```bash
        terraform validate
        ```
     
        Terraform will show any errors found in your configuration files.

  1. Confirm resource changes.

     1. Run this command to view the planned changes:
     
        ```bash
        terraform plan
        ```
     
        If you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
     
     1. If everything looks correct, apply the changes:
        1. Run this command:
     
           ```bash
           terraform apply
           ```
     
        1. Confirm updating the resources.
        1. Wait for the operation to complete.

  For more information, see [this Terraform provider guide](../../terraform/resources/mdb_kafka_topic.md).

- REST API {#api}

  1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

     ```bash
     export IAM_TOKEN="<IAM_token>"
     ```

  1. Call the [Topic.create](../api-ref/Topic/create.md) method, e.g., via the following [cURL](https://curl.se/) request:

     ```bash
     curl \
       --request POST \
       --header "Authorization: Bearer $IAM_TOKEN" \
       --header "Content-Type: application/json" \
       --url 'https://mdb.api.cloud.yandex.net/managed-kafka/v1/clusters/<cluster_ID>/topics' \
       --data '{
                 "topicSpec": {
                  "name": "<topic_name>",
                  "partitions": "<number_of_partitions>",
                  "replicationFactor": "<replication_factor>"
               }'
     ```

     Where:

     * `topicSpec` stands for the topic settings:

        * `name`: Topic name.
        
            {% note info %}
            
            Use the [Apache Kafka® Admin API](cluster-topics.md#admin-api) if you need to create a topic that starts with `_`. You cannot create such a topic using the Yandex Cloud interfaces.
            
            {% endnote %}
        
        * `partitions`: Number of partitions.
        * `replicationFactor`: Replication factor.

     You can request the cluster ID with the [list of clusters in the folder](cluster-list.md#list-clusters).

  1. Check the [server response](../api-ref/Topic/create.md#yandex.cloud.operation.Operation) to make sure your request was successful.

- gRPC API {#grpc-api}

  1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

     ```bash
     export IAM_TOKEN="<IAM_token>"
     ```

  1. Clone the [cloudapi](https://github.com/yandex-cloud/cloudapi) repository:
     
     ```bash
     cd ~/ && git clone --depth=1 https://github.com/yandex-cloud/cloudapi
     ```
     
     Below, we assume that the repository contents reside in the `~/cloudapi/` directory.
  1. Call the [TopicService/Create](../api-ref/grpc/Topic/create.md) method, e.g., via the following [gRPCurl](https://github.com/fullstorydev/grpcurl) request:

     ```bash
     grpcurl \
       -format json \
       -import-path ~/cloudapi/ \
       -import-path ~/cloudapi/third_party/googleapis/ \
       -proto ~/cloudapi/yandex/cloud/mdb/kafka/v1/topic_service.proto \
       -rpc-header "Authorization: Bearer $IAM_TOKEN" \
       -d '{
             "cluster_id": "<cluster_ID>",
             "topic_spec": {
                  "name": "<topic_name>",
                  "partitions": {
                    "value": "<number_of_partitions>"
                  },
                  "replication_factor": {
                    "value": "<replication_factor>"
                  }
             }
           }' \
       mdb.api.cloud.yandex.net:443 \
       yandex.cloud.mdb.kafka.v1.TopicService.Create
     ```

     Where:

     * `topic_spec` stands for the topic settings:

        * `name`: Topic name.
        
            {% note info %}
            
            Use the [Apache Kafka® Admin API](cluster-topics.md#admin-api) if you need to create a topic that starts with `_`. You cannot create such a topic using the Yandex Cloud interfaces.
            
            {% endnote %}
        
        * `partitions`: Number of partitions, provided as an object with the `value` field.
        * `replication_factor`: Replication factor, provided as an object with the `value` field.

     You can request the cluster ID with the [list of clusters in the folder](cluster-list.md#list-clusters).

  1. Check the [server response](../api-ref/grpc/Topic/create.md#yandex.cloud.operation.Operation) to make sure your request was successful.

{% endlist %}

{% note info %}

While running, Managed Service for Apache Kafka® can create [service topics](../concepts/topics.md#service-topics). You cannot write user data to such topics.

{% endnote %}

### Updating topic settings {#update-topic}

You cannot reduce the number of partitions in Managed Service for Apache Kafka® topics. You cannot create new partitions if there is not enough storage space.

For more information, see [Minimum storage size](../concepts/storage.md#minimal-storage-size).

{% list tabs group=instructions %}

- Management console {#console}

  1. In the [management console](https://console.yandex.cloud), navigate to the relevant folder.
  1. Navigate to **Managed Service for&nbsp;Kafka**.
  1. Click the name of your cluster and select the **Topics** tab.
  1. Click ![image](../../_assets/console-icons/ellipsis.svg) for the topic in question and select **Edit**.
  1. Change the basic topic settings:
     * Number of topic partitions.
     * Replication factor. This value should not exceed the number of brokers in the cluster. The minimum value is `1`. The maximum value is `3`. The default value is
       * For a cluster with one or two brokers: `1`.
       * For a cluster with three or more brokers: `3`.
  1. Change [additional topic settings](../concepts/settings-list.md#topic-settings).
  1. Click **Save**.

- CLI {#cli}

  If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../cli/quickstart.md#install).

  The folder used by default is the one specified when [creating](../../cli/operations/profile/profile-create.md) the CLI profile. To change the default folder, use the `yc config set folder-id <folder_ID>` command. You can also specify a different folder for any command using `--folder-name` or `--folder-id`. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.

  To update topic settings:
  1. See the description of the CLI command for updating topics:

     ```bash
     yc managed-kafka topic update --help
     ```

  1. Change [topic settings](../concepts/settings-list.md#topic-settings):

     ```bash
     yc managed-kafka topic update <topic_name> \
       --cluster-name <cluster_name> \
       --partitions <number_of_partitions> \
       --replication-factor <replication_factor>
     ```

- Terraform {#tf}

  1. Open the current Terraform configuration file describing your infrastructure.

     For information about creating this file, see [Creating a cluster Apache Kafka®](cluster-create.md).
  1. Edit the parameter values in the `yandex_mdb_kafka_topic` resource description:

     ```hcl
     resource "yandex_mdb_kafka_topic" "<topic_name>" {
       cluster_id         = "<cluster_ID>"
       name               = "<topic_name>"
       partitions         = <number_of_partitions>
       replication_factor = <replication_factor>
       topic_config {
         compression_type = "<compression_type>"
         flush_messages   = <maximum_number_of_messages_in_memory>
         ...
       }
     }
     ```

  1. Make sure the settings are correct.

     1. In the command line, navigate to the directory that contains the current Terraform configuration files defining the infrastructure.
     1. Run this command:
     
        ```bash
        terraform validate
        ```
     
        Terraform will show any errors found in your configuration files.

  1. Confirm resource changes.

     1. Run this command to view the planned changes:
     
        ```bash
        terraform plan
        ```
     
        If you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
     
     1. If everything looks correct, apply the changes:
        1. Run this command:
     
           ```bash
           terraform apply
           ```
     
        1. Confirm updating the resources.
        1. Wait for the operation to complete.

  For more information, see [this Terraform provider guide](../../terraform/resources/mdb_kafka_topic.md).

- REST API {#api}

  1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

     ```bash
     export IAM_TOKEN="<IAM_token>"
     ```

  1. Call the [Topic.update](../api-ref/Topic/update.md) method, e.g., via the following [cURL](https://curl.se/) request:

     {% note warning %}
     
     The API method will assign default values to all the parameters of the object you are modifying unless you explicitly provide them in your request. To avoid this, list the settings you want to change in the `updateMask` parameter as a single comma-separated string.
     
     {% endnote %}

     ```bash
     curl \
       --request PATCH \
       --header "Authorization: Bearer $IAM_TOKEN" \
       --header "Content-Type: application/json" \
       --url 'https://mdb.api.cloud.yandex.net/managed-kafka/v1/clusters/<cluster_ID>/topics/<topic_name>' \
       --data '{
                 "clusterId": "<cluster_ID>",
                 "updateMask": "topicSpec.partitions,topicSpec.replicationFactor,topicSpec.topicConfig_2_8.<setting_1>,...,topicSpec.topicConfig_2_8.<setting_N>,topicSpec.topicConfig_3.<setting_1>,...,topicSpec.topicConfig_3.<setting_N>",
                 "topicSpec": {
                   "partitions": "<number_of_partitions>",
                   "replicationFactor": "<replication_factor>",
                   "topicConfig_3": {
                     "<setting_1_for_Apache Kafka®_3.x_topic>": "<value_1>",
                     "<setting_2_for_Apache Kafka®_3.x_topic>": "<value_2>",
                     ...
                     "<setting_N_for_Apache Kafka®_3.x_topic>": "<value_N>"
                   }
                 } 
               }'
     ```

     Where:

     * `updateMask`: Comma-separated string of settings to update.

       In this case, list all topic settings to update.

     * `topicSpec` stands for the new topic settings:

        * `partitions`: Number of partitions.
        * `replicationFactor`: Replication factor.
        * `topicConfig_3`: Topic settings for Apache Kafka® `3.x`. Enter each setting on a new line, separated by commas.
        
           See [Settings for individual topics](../concepts/settings-list.md#topic-settings) for descriptions and possible values of the settings.

     You can request the cluster ID with the [list of clusters in the folder](cluster-list.md#list-clusters).

  1. Check the [server response](../api-ref/Topic/update.md#yandex.cloud.operation.Operation) to make sure your request was successful.

- gRPC API {#grpc-api}

  1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

     ```bash
     export IAM_TOKEN="<IAM_token>"
     ```

  1. Clone the [cloudapi](https://github.com/yandex-cloud/cloudapi) repository:
     
     ```bash
     cd ~/ && git clone --depth=1 https://github.com/yandex-cloud/cloudapi
     ```
     
     Below, we assume that the repository contents reside in the `~/cloudapi/` directory.
  1. Call the [TopicService/Update](../api-ref/grpc/Topic/update.md) method, e.g., via the following [gRPCurl](https://github.com/fullstorydev/grpcurl) request:

     {% note warning %}
     
     The API method will assign default values to all the parameters of the object you are modifying unless you explicitly provide them in your request. To avoid this, list the settings you want to change in the `update_mask` parameter as an array of `paths[]` strings.
     
     {% cut "Format for listing settings" %}
     
     ```yaml
     "update_mask": {
         "paths": [
             "<setting_1>",
             "<setting_2>",
             ...
             "<setting_N>"
         ]
     }
     ```
     
     {% endcut %}
     
     {% endnote %}

     ```bash
     grpcurl \
       -format json \
       -import-path ~/cloudapi/ \
       -import-path ~/cloudapi/third_party/googleapis/ \
       -proto ~/cloudapi/yandex/cloud/mdb/kafka/v1/topic_service.proto \
       -rpc-header "Authorization: Bearer $IAM_TOKEN" \
       -d '{
             "cluster_id": "<cluster_ID>",
             "topic_name": "<topic_name>",
             "update_mask": {
               "paths": [
                 "topic_spec.partitions",
                 "topic_spec.replication_factor",
                 "topic_spec.topic_config_2_8.<setting_1>",
                 ...,
                 "topic_spec.topic_config_2_8.<setting_N>",
                 "topic_spec.topic_config_3.<setting_1>",
                 ...,
                 "topic_spec.topic_config_3.<setting_N>"
               ]
             },
             "topic_spec": {
                  "partitions": {
                    "value": "<number_of_partitions>"
                  },
                  "replication_factor": {
                    "value": "<replication_factor>"
                  },
                   "topic_config_3": {
                     "<setting_1_for_Apache Kafka®_3.x_topic>": "<value_1>",
                     "<setting_2_for_Apache Kafka®_3.x_topic>": "<value_2>",
                     ...
                     "<setting_N_for_Apache Kafka®_3.x_topic>": "<value_N>"
                   }
             }
           }' \
       mdb.api.cloud.yandex.net:443 \
       yandex.cloud.mdb.kafka.v1.TopicService.Update
     ```

     Where:

     * `update_mask`: List of settings to update as an array of strings (`paths[]`).

       In this case, list all topic settings to update.

     * `topic_spec` stands for the new topic settings:

        * `partitions`: Number of partitions, provided as an object with the `value` field.
        * `replication_factor`: Replication factor, provided as an object with the `value` field.
        * `topic_config_3`: Topic settings for Apache Kafka® `3.x`. Enter each setting on a new line, separated by commas.
        
           See [Settings for individual topics](../concepts/settings-list.md#topic-settings) for descriptions and possible values of the settings.

     You can request the cluster ID with the [list of clusters in the folder](cluster-list.md#list-clusters).

  1. Check the [server response](../api-ref/grpc/Topic/update.md#yandex.cloud.operation.Operation) to make sure your request was successful.

{% endlist %}

### Getting a list of topics in a cluster {#list-topics}

{% list tabs group=instructions %}

- Management console {#console}

  1. In the [management console](https://console.yandex.cloud), navigate to the relevant folder.
  1. Navigate to **Managed Service for&nbsp;Kafka**.
  1. Click the cluster name and navigate to the **Topics** tab.

- CLI {#cli}

  If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../cli/quickstart.md#install).

  The folder used by default is the one specified when [creating](../../cli/operations/profile/profile-create.md) the CLI profile. To change the default folder, use the `yc config set folder-id <folder_ID>` command. You can also specify a different folder for any command using `--folder-name` or `--folder-id`. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.

  To get a list of topics, run the following command:

  ```bash
  yc managed-kafka topic list --cluster-name <cluster_name>
  ```

- REST API {#api}

  1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

     ```bash
     export IAM_TOKEN="<IAM_token>"
     ```

  1. Call the [Topic.list](../api-ref/Topic/list.md) method, e.g., via the following [cURL](https://curl.se/) request:

     ```bash
     curl \
       --request GET \
       --header "Authorization: Bearer $IAM_TOKEN" \
       --url 'https://mdb.api.cloud.yandex.net/managed-kafka/v1/clusters/<cluster_ID>/topics'
     ```

     You can request the cluster ID with the [list of clusters in the folder](cluster-list.md#list-clusters).

  1. Check the [server response](../api-ref/Topic/list.md#yandex.cloud.mdb.kafka.v1.ListTopicsResponse) to make sure your request was successful.

- gRPC API {#grpc-api}

  1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

     ```bash
     export IAM_TOKEN="<IAM_token>"
     ```

  1. Clone the [cloudapi](https://github.com/yandex-cloud/cloudapi) repository:
     
     ```bash
     cd ~/ && git clone --depth=1 https://github.com/yandex-cloud/cloudapi
     ```
     
     Below, we assume that the repository contents reside in the `~/cloudapi/` directory.
  1. Call the [TopicService/List](../api-ref/grpc/Topic/list.md) method, e.g., via the following [gRPCurl](https://github.com/fullstorydev/grpcurl) request:

     ```bash
     grpcurl \
       -format json \
       -import-path ~/cloudapi/ \
       -import-path ~/cloudapi/third_party/googleapis/ \
       -proto ~/cloudapi/yandex/cloud/mdb/kafka/v1/topic_service.proto \
       -rpc-header "Authorization: Bearer $IAM_TOKEN" \
       -d '{
             "cluster_id": "<cluster_ID>"
           }' \
       mdb.api.cloud.yandex.net:443 \
       yandex.cloud.mdb.kafka.v1.TopicService.List
     ```

     You can request the cluster ID with the [list of clusters in the folder](cluster-list.md#list-clusters).

  1. Check the [server response](../api-ref/grpc/Topic/list.md#yandex.cloud.mdb.kafka.v1.ListTopicsResponse) to make sure your request was successful.

{% endlist %}

### Getting detailed information about a topic {#get-topic}

{% list tabs group=instructions %}

- Management console {#console}

  1. In the [management console](https://console.yandex.cloud), navigate to the relevant folder.
  1. Navigate to **Managed Service for&nbsp;Kafka**.
  1. Click the cluster name and navigate to the **Topics** tab.
  1. Click the topic name.

- CLI {#cli}

  If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../cli/quickstart.md#install).

  The folder used by default is the one specified when [creating](../../cli/operations/profile/profile-create.md) the CLI profile. To change the default folder, use the `yc config set folder-id <folder_ID>` command. You can also specify a different folder for any command using `--folder-name` or `--folder-id`. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.

  To get detailed information about a topic, run the following command:

  ```bash
  yc managed-kafka topic get <topic_name> --cluster-name <cluster_name>
  ```

- REST API {#api}

  1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

     ```bash
     export IAM_TOKEN="<IAM_token>"
     ```

  1. Call the [Topic.list](../api-ref/Topic/get.md) method, e.g., via the following [cURL](https://curl.se/) request:

     ```bash
     curl \
       --request GET \
       --header "Authorization: Bearer $IAM_TOKEN" \
       --url 'https://mdb.api.cloud.yandex.net/managed-kafka/v1/clusters/<cluster_ID>/topics/<topic_name>'
     ```

     You can get the cluster ID with the [list of clusters in the folder](cluster-list.md#list-clusters), and the topic name, with the [list of topics in the cluster](#list-topics).

  1. Check the [server response](../api-ref/Topic/get.md#yandex.cloud.mdb.kafka.v1.Topic) to make sure your request was successful.

- gRPC API {#grpc-api}

  1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

     ```bash
     export IAM_TOKEN="<IAM_token>"
     ```

  1. Clone the [cloudapi](https://github.com/yandex-cloud/cloudapi) repository:
     
     ```bash
     cd ~/ && git clone --depth=1 https://github.com/yandex-cloud/cloudapi
     ```
     
     Below, we assume that the repository contents reside in the `~/cloudapi/` directory.
  1. Call the [TopicService/Get](../api-ref/grpc/Topic/get.md) method, e.g., via the following [gRPCurl](https://github.com/fullstorydev/grpcurl) request:

     ```bash
     grpcurl \
       -format json \
       -import-path ~/cloudapi/ \
       -import-path ~/cloudapi/third_party/googleapis/ \
       -proto ~/cloudapi/yandex/cloud/mdb/kafka/v1/topic_service.proto \
       -rpc-header "Authorization: Bearer $IAM_TOKEN" \
       -d '{
             "cluster_id": "<cluster_ID>",
             "topic_name": "<topic_name>"
           }' \
       mdb.api.cloud.yandex.net:443 \
       yandex.cloud.mdb.kafka.v1.TopicService.Get
     ```

     You can request the cluster ID with the [list of clusters in the folder](cluster-list.md#list-clusters), and the topic name, with the [list of topics in the cluster](#list-topics).

  1. Check the [server response](../api-ref/grpc/Topic/list.md#yandex.cloud.mdb.kafka.v1.Topic) to make sure your request was successful.

{% endlist %}

### Importing a topic to Terraform {#import-topic}

You can import the existing cluster topics to manage them with Terraform.

{% list tabs group=instructions %}

- Terraform {#tf}

    1. In the Terraform configuration file, specify the topic you want to import:

        ```hcl
        resource "yandex_mdb_kafka_topic" "<topic_name>" {}
        ```

    1. Run the following command to import your topic:

        ```hcl
        terraform import yandex_mdb_kafka_topic.<topic_name> <cluster_ID>:<topic_name>
        ```

        To learn more about importing topics, see [this Terraform provider guide](../../terraform/resources/mdb_kafka_topic.md#import).

{% endlist %}

### Transferring information about created topics to the Terraform state file {#move-info-topic}

When upgrading to a new Terraform provider version, the state file and configuration file may differ regarding created topics, with obsolete `topic` attributes and new `yandex_mdb_kafka_topic` resources. To eliminate the differences, delete the `topic` attributes and transfer information about the created `yandex_mdb_kafka_topic` resources to the `.tfstate` state file. You can do this in one of the two methods described below.

#### First method {#first}

{% list tabs %}

- Terraform {#tf}

  1. Delete the cluster information from the `.tfstate` file using this command:

     ```bash
     terraform state rm yandex_mdb_kafka_cluster.<cluster_name>
     ```

  1. Edit the Terraform configuration file:
     * Delete the `topic` attributes from the `yandex_mdb_kafka_cluster` resource.
     * [Add](../../terraform/resources/mdb_kafka_topic.md) new `yandex_mdb_kafka_topic` resources.

      {% cut "Example of the updated configuration file" %}
        
      ```hcl
      resource "yandex_mdb_kafka_cluster" "this" {
        name = "terraform-test"
        environment = "PRODUCTION"
        network_id = data.yandex_vpc_network.this.id

        config {
          version = "3.4"
          brokers_count = 1
          zones = ["ru-central1-a"]
          kafka {
            resources {
              resource_preset_id = "s2.small"
              disk_size = 30
              disk_type_id = "network-ssd"
            }
            kafka_config {
              log_segment_bytes = 104857600
            }
          }
        }
      }

      resource "yandex_mdb_kafka_topic" "topic1" {
        cluster_id = yandex_mdb_kafka_cluster.this.id
        name = "topic1"
        partitions = 3
        replication_factor = 1
      }


      resource "yandex_mdb_kafka_topic" "topic2" {
        cluster_id = yandex_mdb_kafka_cluster.this.id
        name = "topic2"
        partitions = 3
        replication_factor = 1
      }
      ```
        
      {% endcut %}
        
  1. Import the cluster and topics:

     ```bash
     terraform import yandex_mdb_kafka_cluster.<cluster_name> <cluster_ID>
     terraform import yandex_mdb_kafka_topic.<topic_name> <cluster_ID>:<topic_name>
     terraform import yandex_mdb_kafka_topic.<topic_name> <cluster_ID>:<topic_name>
     ```
    
  1. Check the result using this command:
     
     ```bash
     terraform plan
     ```
     
     If the state file matches the configuration, the terminal displays this message:
     
     ```bash
     No changes. Infrastructure is up-to-date.
     ```
     
     Terraform will inform you if the state file does not match the configuration.

{% endlist %}

#### Second method {#second}

{% list tabs %}

- Terraform {#tf}

  1. Download the `.tfstate` file using this command:

     ```bash
     terraform state pull
     ```
    
  1. Open the downloaded file in any text editor and delete the `topic` attributes from the `yandex_mdb_kafka_cluster` resource.
  1. Push the updated state file using this command:

     ```bash
     terraform state push
     ```
    
  1. Edit the Terraform configuration file:
     * Delete the `topic` attributes from the `yandex_mdb_kafka_cluster` resource.
     * [Add](../../terraform/resources/mdb_kafka_topic.md) new `yandex_mdb_kafka_topic` resources.

      {% cut "Example of the updated configuration file" %}
        
      ```hcl
      resource "yandex_mdb_kafka_cluster" "this" {
        name = "terraform-test"
        environment = "PRODUCTION"
        network_id = data.yandex_vpc_network.this.id

        config {
          version = "3.4"
          brokers_count = 1
          zones = ["ru-central1-a"]
          kafka {
            resources {
              resource_preset_id = "s2.small"
              disk_size = 30
              disk_type_id = "network-ssd"
            }
            kafka_config {
              log_segment_bytes = 104857600
            }
          }
        }
      }

      resource "yandex_mdb_kafka_topic" "topic1" {
        cluster_id = yandex_mdb_kafka_cluster.this.id
        name = "topic1"
        partitions = 3
        replication_factor = 1
      }


      resource "yandex_mdb_kafka_topic" "topic2" {
        cluster_id = yandex_mdb_kafka_cluster.this.id
        name = "topic2"
        partitions = 3
        replication_factor = 1
      }
      ```
        
      {% endcut %}

  1. Import the topics:

     ```bash
     terraform import yandex_mdb_kafka_topic.<topic_name> <cluster_ID>:<topic_name>
     terraform import yandex_mdb_kafka_topic.<topic_name> <cluster_ID>:<topic_name>
     ``` 
    
  1. Check the result using this command:
     
     ```bash
     terraform plan
     ```
     
     If the state file matches the configuration, the terminal displays this message:
     
     ```bash
     No changes. Infrastructure is up-to-date.
     ```
     
     Terraform will inform you if the state file does not match the configuration.

{% endlist %}

### Deleting a topic {#delete-topic}

{% note info %}

Permissions granted to a [user](cluster-accounts.md) for a topic persist even after the topic is [deleted](cluster-topics.md#delete-topic). If you do not [revoke the permissions](cluster-accounts.md#revoke-permission) after topic deletion, the user will be able to access a [newly created topic](cluster-topics.md#create-topic) with the same name without reassigning permissions.

{% endnote %}

{% list tabs group=instructions %}

- Management console {#console}

  1. In the [management console](https://console.yandex.cloud), navigate to the relevant folder.
  1. Navigate to **Managed Service for&nbsp;Kafka**.
  1. Click the cluster name and navigate to the **Topics** tab.
  1. Click ![image](../../_assets/console-icons/ellipsis.svg) for the topic in question and select **Delete topic**.
  1. In the window that opens, click **Delete**.

- CLI {#cli}

  If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../cli/quickstart.md#install).

  The folder used by default is the one specified when [creating](../../cli/operations/profile/profile-create.md) the CLI profile. To change the default folder, use the `yc config set folder-id <folder_ID>` command. You can also specify a different folder for any command using `--folder-name` or `--folder-id`. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.

  To delete a topic:
  1. See the description of the CLI command for deleting topics:

     ```bash
     yc managed-kafka topic delete --help
     ```

  1. Delete a topic:

     ```bash
     yc managed-kafka topic delete <topic_name> --cluster-name <cluster_name>
     ```

- Terraform {#tf}

  1. Open the current Terraform configuration file describing your infrastructure.

     For information about creating this file, see [Creating a cluster Apache Kafka®](cluster-create.md).
  1. Delete the `yandex_mdb_kafka_topic` resource with the topic description.
  1. Make sure the settings are correct.

     1. In the command line, navigate to the directory that contains the current Terraform configuration files defining the infrastructure.
     1. Run this command:
     
        ```bash
        terraform validate
        ```
     
        Terraform will show any errors found in your configuration files.

  1. Confirm resource changes.

     1. Run this command to view the planned changes:
     
        ```bash
        terraform plan
        ```
     
        If you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
     
     1. If everything looks correct, apply the changes:
        1. Run this command:
     
           ```bash
           terraform apply
           ```
     
        1. Confirm updating the resources.
        1. Wait for the operation to complete.

  For more information, see [this Terraform provider guide](../../terraform/resources/mdb_kafka_topic.md).

- REST API {#api}

  1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

     ```bash
     export IAM_TOKEN="<IAM_token>"
     ```

  1. Use the [Topic.delete](../api-ref/Topic/delete.md) method and send the following request, e.g., via [cURL](https://curl.se/):

     ```bash
     curl \
       --request DELETE \
       --header "Authorization: Bearer $IAM_TOKEN" \
       --url 'https://mdb.api.cloud.yandex.net/managed-kafka/v1/clusters/<cluster_ID>/topics/<topic_name>'
     ```

     You can get the cluster ID with the [list of clusters in the folder](cluster-list.md#list-clusters), and the topic name, with the [list of topics in the cluster](#list-topics).

  1. Check the [server response](../api-ref/Topic/delete.md#yandex.cloud.operation.Operation) to make sure your request was successful.

- gRPC API {#grpc-api}

  1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

     ```bash
     export IAM_TOKEN="<IAM_token>"
     ```

  1. Clone the [cloudapi](https://github.com/yandex-cloud/cloudapi) repository:
     
     ```bash
     cd ~/ && git clone --depth=1 https://github.com/yandex-cloud/cloudapi
     ```
     
     Below, we assume that the repository contents reside in the `~/cloudapi/` directory.
  1. Call the [TopicService/Delete](../api-ref/grpc/Topic/delete.md) method, e.g., via the following [gRPCurl](https://github.com/fullstorydev/grpcurl) request:

     ```bash
     grpcurl \
       -format json \
       -import-path ~/cloudapi/ \
       -import-path ~/cloudapi/third_party/googleapis/ \
       -proto ~/cloudapi/yandex/cloud/mdb/kafka/v1/topic_service.proto \
       -rpc-header "Authorization: Bearer $IAM_TOKEN" \
       -d '{
             "cluster_id": "<cluster_ID>",
             "topic_name": "<topic_name>"
           }' \
       mdb.api.cloud.yandex.net:443 \
       yandex.cloud.mdb.kafka.v1.TopicService.Delete
     ```

     You can request the cluster ID with the [list of clusters in the folder](cluster-list.md#list-clusters), and the topic name, with the [list of topics in the cluster](#list-topics).

  1. Check the [server response](../api-ref/grpc/Topic/delete.md#yandex.cloud.operation.Operation) to make sure your request was successful.

{% endlist %}

## Managing topics via the Apache Kafka® Admin API {#admin-api}

To manage topics via the Apache Kafka® Admin API:
1. [Create](cluster-accounts.md#create-account) an admin user with either the `ACCESS_ROLE_ADMIN` or `ACCESS_ROLE_TOPIC_ADMIN` role in the cluster. Learn more about the permissions you get with each role [here](../concepts/account-roles.md).
1. Manage topics on behalf of this user by making requests to the Apache Kafka® Admin API. Learn how to use the Admin API in your programming language guide.

For more information about working with the Admin API and the existing limitations, see [Managing topics and partitions](../concepts/topics.md#management) and [this Apache Kafka® guide](https://kafka.apache.org/42/apis/#admin-api).