How to Create and Delete Kafka Topics

How to Create and Delete Kafka Topics: A Step-by-Step Guide
Apache Kafka create and delete topic command example

How to Create and Delete Kafka Topics: A Complete CLI Guide

Apache Kafka is the backbone of modern data streaming. While setting up the server is a one-time task, managing Topics is a daily responsibility for Data Engineers.

In this guide, I will demonstrate exactly how to create and delete Kafka topics using the command line (CLI), covering the syntax for both Linux and Windows environments.


🔹 What Is an Apache Kafka Topic?

Think of a Kafka topic as a folder in a filesystem, but for events. It is the specific category where producers push data and consumers read from.

To understand the commands below, you need to know three concepts:

  • Topic: The category name (e.g., "User_Logins").
  • Partitions: Kafka splits topics into "parts" to allow parallel processing. More partitions = higher throughput.
  • Replication Factor: How many copies of the data exist. If one server dies, the data is safe on another.

🔹 Prerequisites

You must have Zookeeper and the Kafka Broker running. If you haven't installed Kafka yet, please check my previous guide first:

👉 Setup Guide: How to Set Up Apache Kafka (Step-by-Step)


🛠️ Creating a Kafka Topic

1. Basic Creation Command

The following command creates a topic named demo-topic with basic settings.

📌 Linux / macOS

bin/kafka-topics.sh --create \
--topic demo-topic \
--bootstrap-server localhost:9092 \
--partitions 3 \
--replication-factor 1

📌 Windows

bin\windows\kafka-topics.bat --create ^
--topic demo-topic ^
--bootstrap-server localhost:9092 ^
--partitions 3 ^
--replication-factor 1
Note: We use localhost:9092 here. If you are running Kafka in Docker or a cloud server, replace this with your public IP or DNS.

2. Advanced Creation (With Retention)

In production, you often need to control how long data lives. Here is how to create a topic that deletes data after 7 days:

Linux

bin/kafka-topics.sh --create \
--topic sensor-data \
--bootstrap-server localhost:9092 \
--partitions 6 \
--replication-factor 2 \
--config retention.ms=604800000

604800000ms equals exactly 7 days.


📋 Listing Kafka Topics

To verify your topic was created successfully, run the list command:

# Linux
bin/kafka-topics.sh --list --bootstrap-server localhost:9092

# Windows
bin\windows\kafka-topics.bat --list --bootstrap-server localhost:9092

🗑️ Deleting a Kafka Topic

If you made a typo or need to clean up, use the delete flag:

Linux

bin/kafka-topics.sh --delete \
--topic demo-topic \
--bootstrap-server localhost:9092

Windows

bin\windows\kafka-topics.bat --delete ^
--topic demo-topic ^
--bootstrap-server localhost:9092

⚡ Troubleshooting Common Errors

Based on my experience deploying Kafka, here are the most common errors you will face:

Error: "Replication factor: 2 larger than available brokers: 1"

Cause: You tried to create a topic with --replication-factor 2, but you only have 1 Kafka server running on your machine.

Fix: Change --replication-factor to 1 or start more broker instances.

Error: "Topic already exists"

Cause: You ran the create command twice.

Fix: Use the --list command to check current topics before creating new ones.


🎥 Watch the Practical Demo

I have recorded a complete session showing these commands in action on a real server. Watch to see the expected output for every command:


📌 Found this helpful? Follow IoT Station for more Data Engineering tutorials.

Comments

Popular posts from this blog

How to Import CSV Files into PostgreSQL Automatically

Apache NiFi ETL Tutorial for Beginners | Installation & Data Pipeline Basics

ESP32 TFT LCD 240x240 Tutorial-Display Images & DHT11 Sensor

Contact Form

Name

Email *

Message *