Is there a way to delete all the data from a topic or delete the topic before every run?
Can I modify the KafkaConfig.scala file to change the logRetentionHours
property? Is there a way the messages gets deleted as soon as the consumer reads it?
I am using producers to fetch the data from somewhere and sending the data to a particular topic where a consumer consumes, can I delete all the data from that topic on every run? I want only new data every time in the topic. Is there a way to reinitialize the topic somehow?
log.retention.hours
and addlog.retention.ms=1000
. It would keep the record on Kafka Topic for only one second.log.retention.hours
to your desired figure.Don't think it is supported yet.Take a look at this JIRA issue "Add delete topic support".To delete manually:
log.dir
attribute in kafka config file ) as well the zookeeper dataFor any given topic what you can do is
/tmp/kafka-logs/MyTopic-0
where/tmp/kafka-logs
is specified by thelog.dir
attributeThis is
NOT
a good and recommended approach but it should work. In the Kafka broker config file thelog.retention.hours.per.topic
attribute is used to defineThe number of hours to keep a log file before deleting it for some specific topic
From the Kafka Documentation :
For finding the start offset to read in Kafka 0.8 Simple Consumer example they say
You can also find the example code there for managing the offset at your consumer end.
As a dirty workaround, you can adjust per-topic runtime retention settings, e.g.
bin/kafka-topics.sh --zookeeper localhost:2181 --alter --topic my_topic --config retention.bytes=1
(retention.bytes=0 might also work)After a short while kafka should free the space. Not sure if this has any implications compared to re-creating the topic.
ps. Better bring retention settings back, once kafka done with cleaning.
You can also use
retention.ms
to persist historical dataTested with kafka 0.10
Note : if you are deleting topic folder/s inside kafka-logs but not from zookeeper-data folder, then you will see topics are still there.
For brew users
If you're using
brew
like me and wasted a lot of time searching for the infamouskafka-logs
folder, fear no more. (and please do let me know if that works for you and multiple different versions of Homebrew, Kafka etc :) )You're probably going to find it under:
Location:
/usr/local/var/lib/kafka-logs
How to actually find that path
(this is also helpful for basically every app you install through brew)
1)
brew services list
2) Open and read that
plist
you found above3) Find the line defining
server.properties
location open it, in my case:/usr/local/etc/kafka/server.properties
4) Look for the
log.dirs
line:5) Go to that location and delete the logs for the topics you wish
6) Restart Kafka with
brew services restart kafka
As I mentioned here Purge Kafka Queue:
Tested in Kafka 0.8.2, for the quick-start example: First, Add one line to server.properties file under config folder:
then, you can run this command: