Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delete topic not working for kafka java client 2.1.0

I want to delete a kafka topic programatically and struggling a lot to make it work. Am using below maven dependency.

  <dependency>
        <groupId>org.apache.kafka</groupId>
        <artifactId>kafka-clients</artifactId>
        <version>2.1.0</version>
    </dependency>

Am using KafkaAdminClient. Here is my code,

AdminClient admin = KafkaAdminClient.create(getProperties(configuration.getKafkaConnectionString())));

private static Properties getProperties(String kafkaConnectionString) {
    Properties config = new Properties();
    config.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaConnectionString);
    return config;
}

Code to delete topic:

public void deleteTopic(final String... topicNames) {
    admin.deleteTopics(Arrays.asList(topicNames));
    log.info("Topics '{}' deleted.", topicNames);
}

I have delete.topic.enable=true property in server.properties for kafka. There are no exception. Not sure why its not working. Any help is appreciated.

like image 467
ProgrammerBoy Avatar asked Oct 16 '25 19:10

ProgrammerBoy


1 Answers

  • Make sure producer is not using that topic.

  • Since deleteTopics() method returns a future , wait for future task to complete ( In case if you are using on-demand tasks, Since main thread will not wait for future to complete if we don't do it manually.).

    public void deleteKafkaTopics(List<String> kafkaTopics) {
    
         DeleteTopicsResult deleteTopicsResult = kafkaAdminClient.deleteTopics(kafkaTopics);
    
         while (!deleteTopicsResult.all().isDone()) {
    
             // Wait for future task to complete
    
         }
     }
    

Note: If you are using windows machine you may face AccessDeniedException. Already ticket open in kafka issue board (Iam using kafka 2.1) for that fix.So I prefer to use Linux machines which is working fine with Kafka. Also don't forget to set delete.topic.enable = true in config/server.properties of Kafka brokers . For newer versions of kafka it is enabled by default.

like image 162
pvrforpranavvr Avatar answered Oct 18 '25 08:10

pvrforpranavvr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!