Skip to content

Instantly share code, notes, and snippets.

@mjedynak
Last active February 12, 2022 11:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjedynak/e47088b796a29e5c6636117ad38fdb9c to your computer and use it in GitHub Desktop.
Save mjedynak/e47088b796a29e5c6636117ad38fdb9c to your computer and use it in GitHub Desktop.
@Component
class TopicPurger(private val adminClient: AdminClient) {
fun purge(topicName: String) {
val topicDescription = adminClient.describeTopics(listOf(topicName)).all().get()[topicName]
val partitionSize = topicDescription?.partitions()?.size
val recordsToDelete = (0..partitionSize!!).associate { partitionIndex ->
TopicPartition(topicName, partitionIndex) to RecordsToDelete.beforeOffset(-1)
}
adminClient.deleteRecords(recordsToDelete)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment