// Module included in the following assemblies:
//
// ???

[id='proc-scaling-down-a-kafka-cluster-{context}']

= Scaling down a Kafka cluster

This procedure describes how to decrease the number of brokers in a Kafka cluster.

.Prerequisites

* An existing Kafka cluster.
* A _reassignment JSON file_ named `_reassignment.json_` describing how partitions should be reassigned to brokers in the cluster once the broker(s) in the highest numbered `Pod(s)` have been removed.

.Procedure

include::snip-reassign-partitions.adoc[]

. Once all the partition reassignments have finished, the broker(s) being removed should not have responsibility for any of the partitions in the cluster.
You can verify this by checking that the broker's data log directory does not contain any live partition logs.
If the log directory on the broker contains a directory that does not match the extended regular expression `[a-zA-Z0-9.-]+\.[a-z0-9]+-delete$` then the broker still has live partitions and it should not be stopped.
+
You can check this by executing the command:
+
[source,shell,subs=+quotes]
kubectl exec my-cluster-kafka-0 -c kafka -it -- \
  /bin/bash -c \
  "ls -l /var/lib/kafka/kafka-log_<N>_ | grep -E '^d' | grep -vE '[a-zA-Z0-9.-]+\.[a-z0-9]+-delete$'"
+
where _N_ is the number of the `Pod(s)` being deleted.
+
If the above command prints any output then the broker still has live partitions.
In this case, either the reassignment has not finished, or the reassignment JSON file was incorrect.

. Once you have confirmed that the broker has no live partitions you can edit the `Kafka.spec.kafka.replicas` of your `Kafka` resource, which will scale down the `StatefulSet`, deleting the highest numbered broker `Pod(s)`.

.Additional resources
