title: How to delete dynamically created consumer groups in spring boot app
body: <p>I have multiple instances of my spring boot app consuming from a kafka topic. Since I want all instances to get data from all partitions of this topic, I assigned different consumers groups for each instances which would be created dynamically when starting this application.</p>
  <pre><code>@Configuration
  @EnableKafka
  public class KafkaStreamConfig {
  @Bean(&quot;provisioningStreamsBuilderFactoryBean&quot;)
    public StreamsBuilderFactoryBean myStreamsBuilderFactoryBean() {
      String myCGName = &quot;MY-CG-&quot; + UUID.randomUUID().toString();
      Properties streamsConfiguration = new Properties();
      streamsConfiguration.put(APPLICATION_ID_CONFIG, myCGName); // setting consumer group name
      // setting other props
      StreamsBuilderFactoryBean streamsBuilderFactoryBean = new StreamsBuilderFactoryBean();
      streamsBuilderFactoryBean.setStreamsConfiguration(streamsConfiguration);
      return streamsBuilderFactoryBean;
    }
  }
  </code></pre>
  <p>So every time an instance restarts or a new instance is created, a new consumer group is created. And this's the consumer which reads from my topic.</p>
  <pre><code>@Component
  public class MyConsumer {
      @Autowired
      private StreamsBuilder streamsBuilder;
  
      @PostConstruct
      public void consume() {
        final KStream&lt;String, GenericRecord&gt; events = streamsBuilder.stream(&quot;my-topic&quot;);
        events
        .selectKey((key, record) -&gt; record.get(&quot;id&quot;).toString())
        .foreach((id, record) -&gt; {
            // some computations with the events consumed
        });
      }
  }
  </code></pre>
  <p>Now because of these dynamically created consumer groups stay on, and since they're not used in my application once an instance restarts, these don't consume messages anymore and show a lot of lag and hence give rise to false alerts.</p>
  <p>So I'd like to delete these consumer groups when the application shuts down with Kafka's AdminClient api. I was thinking of trying to delete it in a shutdown hook like in a method annotated with <code>@PreDestroy</code> inside <strong>MyConsumer</strong> class like this:</p>
  <pre><code>@PreDestroy
  public void destroyMYCG() {
    try (AdminClient admin = KafkaAdminClient.create(properties)) {
      DeleteConsumerGroupsResult deleteConsumerGroupsResult = admin.deleteConsumerGroups(Collections.singletonList(provGroupName));
      KafkaFuture&lt;Void&gt; future =  deleteConsumerGroupsResult.all();
      future.whenComplete((aVoid, throwable) -&gt; {
        System.out.println(&quot;EXCEPTION :: &quot; + ExceptionUtils.getStackTrace(throwable));
      });
    }
    System.out.println(getClass().getCanonicalName() + &quot; :: DESTROYING :: &quot; + provGroupName);
  }
  </code></pre>
  <p>but I'm getting this exception if I tried that and consumer groups still shows up in the list of consumer groups:</p>
  <blockquote>
  <p>org.apache.kafka.common.errors.TimeoutException: The AdminClient thread is not accepting new calls.</p>
  </blockquote>
  <p>Can someone please help me with this?</p>