How to Delete a Topic in Kafka: Simple Steps
To delete a topic in Kafka, use the
kafka-topics.sh --delete --topic <topic_name> --bootstrap-server <broker_address> command. Make sure topic deletion is enabled in your Kafka broker configuration with delete.topic.enable=true.Syntax
The command to delete a Kafka topic uses the kafka-topics.sh tool with these options:
--delete: tells Kafka to delete the topic.--topic <topic_name>: specifies the name of the topic to delete.--bootstrap-server <broker_address>: points to the Kafka broker to connect to.
Example: kafka-topics.sh --delete --topic my-topic --bootstrap-server localhost:9092
bash
kafka-topics.sh --delete --topic <topic_name> --bootstrap-server <broker_address>
Example
This example deletes a topic named test-topic from a Kafka broker running on localhost:9092. It assumes delete.topic.enable=true is set in the broker config.
bash
kafka-topics.sh --delete --topic test-topic --bootstrap-server localhost:9092Output
Topic test-topic is marked for deletion.
Common Pitfalls
- Topic deletion disabled: If
delete.topic.enableis false in the broker config, the delete command will mark the topic but it won't be removed. - Topic not found: Trying to delete a non-existent topic will show an error.
- Delay in deletion: Deletion is asynchronous and may take time to complete.
Always check broker settings and topic existence before deleting.
bash
## Wrong: Deleting without enabling deletion # Broker config missing delete.topic.enable=true kafka-topics.sh --delete --topic my-topic --bootstrap-server localhost:9092 ## Right: Enable deletion in server.properties # Add or set: delete.topic.enable=true # Restart broker and then delete topic kafka-topics.sh --delete --topic my-topic --bootstrap-server localhost:9092
Quick Reference
| Option | Description |
|---|---|
| --delete | Delete the specified topic |
| --topic | Name of the topic to delete |
| --bootstrap-server | Kafka broker address to connect |
| delete.topic.enable | Broker config to enable topic deletion (must be true) |
Key Takeaways
Use kafka-topics.sh with --delete and --topic options to delete a Kafka topic.
Ensure delete.topic.enable=true is set in the Kafka broker configuration before deleting.
Topic deletion is asynchronous and may take some time to complete.
Deleting a non-existent topic will cause an error; verify topic existence first.
Restart the Kafka broker after changing delete.topic.enable to apply the setting.