<p>I have written a bash script to get  all deployments on a kubernetes cluster.
I have a command to scale all the deployments to zero.
The challenge I am having is that, I want to be able to loop through all the deployments and save their name and number of replicas so I scale them back to the original after scaling down.</p>
<p>How do I achieve that?
this is what I have done so far.</p>
<pre><code>$ kubectl get deployments
$ kubectl scale deploy -n default  --replicas=0 --all
</code></pre>

Clement Okyere
<p>You could annotate resources for the previous state of replications. Then you could use the below commands to change replicas.</p>
<pre><code># annotate first 
kubectl get deploy -o jsonpath='{range .items[*]}{&quot;kubectl annotate --overwrite deploy &quot;}{@.metadata.name}{&quot; previous-size=&quot;}{@.spec.replicas}{&quot; \n&quot;}{end}' | sh

# scale to 0
kubectl scale --replicas=0 $(kubectl get deploy -o name) 

## scaleback 
kubectl get deploy -o jsonpath='{range .items[*]}{&quot;kubectl scale deploy &quot;}{@.metadata.name}{&quot; --replicas=&quot;}{.metadata.annotations.previous-size}{&quot;\n&quot;}{end}' | sh
</code></pre>
