Kafka's Admin.deleteTopics API unfortunately returns only after the request has been received - what only means that the topics are scheduled for deletion by the cluster, but not necessarily deleted now.
To show that as an example, the following code can often throw:
final var newTopic = new NewTopic("aaa", Optional.empty(), Optional.empty());
this.admin.createTopics(Collections.singleton(newTopic), opt).all().get();
this.admin.deleteTopics(Arrays.asList("aaa")).all().get();
this.admin.listTopics( ).names().get().contains("aaa"); // Returns 'false'.
this.admin.createTopics(Collections.singleton(newTopic), opt).all().get(); // <- throws
with an exception:
java.util.concurrent.ExecutionException: org.apache.kafka.common.errors.TopicExistsException: Topic 'aaa' is marked for deletion.
at java.base/java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:395)
at java.base/java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1999)
at org.apache.kafka.common.internals.KafkaFutureImpl.get(KafkaFutureImpl.java:165)
...
Caused by: org.apache.kafka.common.errors.TopicExistsException: Topic 'aaa' is marked for deletion.
Unfortunately Admin.listTopics() that does not help here, the topic stops being visible after the delete request is submitted.
So the question is - is there any programatic way (preferably API) that would allow us to monitor that the topic is really gone?
Kafka (both client & server) version used is 3.2.