0

I need to clear or delete Kafka topics programmatically using C# language. Currently, I have used Confluent.Kafka library for publishing and consuming Kafka topics.

I can delete Kafka topics using the command line like this

kafka-topics.bat --zookeeper 192.108.94.79:2181 --delete --topic test-topic3

Is any library or way available for clearing Kafka topic programmatically using C# language?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Abdul Manaf
  • 4,933
  • 8
  • 51
  • 95
  • Was delete functionality dot added to the library according to [this](https://github.com/confluentinc/confluent-kafka-dotnet/issues/332) – Scrobi Dec 27 '18 at 15:05

2 Answers2

2

In version 1.3.0 of confluent.kafka AdminClient class is internal, so you have to use AdminClientBuilder

Example:

AdminClientBuilder builder = new AdminClientBuilder(new AdminClientConfig() { BootstrapServers =""})

builder.Build();
Ygalbel
  • 5,214
  • 1
  • 24
  • 32
1

We can quickly delete the Kafka topics using Confluent.Kafka library version 1.0.0. But currently, it's in beta release. This library support Kafka admin utilities. Following code helps to clear/delete Kafka topics.

using Confluent.Kafka;
using System;
using System.Collections.Generic;

namespace deleteKafkaTopic
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine($"librdkafka Version: {Library.VersionString} ({Library.Version:X})");
            Console.WriteLine($"Debug Contexts: {string.Join(", ", Library.DebugContexts)}");

            IEnumerable<string> topicList = new List<string>() { "test-topic4" };
            deleteTopics("192.168.64.49:9092", topicList);
        }
        static void deleteTopics(string brokerList, IEnumerable<string> topicNameList)
        {
            using (var adminClient = new AdminClient(new AdminClientConfig { BootstrapServers = brokerList }))
            {
                adminClient.DeleteTopicsAsync(topicNameList, null);
            }
        }
    }
}
Abdul Manaf
  • 4,933
  • 8
  • 51
  • 95