0
0
Kafkadevops~5 mins

Topic deletion and cleanup in Kafka - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you need to remove old or unused Kafka topics to keep your system clean and avoid wasting storage. Deleting topics helps manage resources and keeps your Kafka environment organized.
When a topic is no longer needed and you want to free up disk space.
When you want to remove test or temporary topics created during development.
When cleaning up after a project or service that used specific topics.
When you want to avoid confusion by removing outdated topics from the list.
When you want to reset a topic by deleting and recreating it.
Commands
This command deletes the Kafka topic named 'example-topic' from the Kafka cluster running on localhost:9092. It frees up resources used by this topic.
Terminal
kafka-topics.sh --bootstrap-server localhost:9092 --delete --topic example-topic
Expected OutputExpected
Topic example-topic is marked for deletion.
--delete - Marks the topic for deletion.
--topic - Specifies the name of the topic to delete.
--bootstrap-server - Specifies the Kafka server to connect to.
Lists all topics currently available in the Kafka cluster to verify that 'example-topic' has been deleted.
Terminal
kafka-topics.sh --bootstrap-server localhost:9092 --list
Expected OutputExpected
other-topic-1 other-topic-2
--list - Lists all topics.
--bootstrap-server - Specifies the Kafka server to connect to.
Key Concept

If you remember nothing else from this pattern, remember: deleting a Kafka topic marks it for removal, and you should verify its deletion by listing topics afterward.

Common Mistakes
Trying to delete a topic without enabling topic deletion in Kafka configuration.
Kafka will not delete the topic because the server setting 'delete.topic.enable' is false by default in some versions.
Ensure 'delete.topic.enable=true' is set in the Kafka broker configuration before deleting topics.
Assuming the topic is deleted immediately after running the delete command.
Kafka deletes topics asynchronously; the topic may still appear in the list for a short time.
Wait a few moments and check the topic list again to confirm deletion.
Summary
Use 'kafka-topics.sh --delete' with the topic name to mark a topic for deletion.
Verify deletion by listing topics with 'kafka-topics.sh --list'.
Make sure Kafka broker allows topic deletion by setting 'delete.topic.enable=true'.