Clean up "Replica Sets" when updating deployments?

A revision on Kévin's post above as I can't comment yet :D

AWK lets us ignore the spacing!

kubectl delete $(kubectl get all | grep replicaset.apps | awk '{if ($2 + $3 + $4 == 0) print $1}')

Here's a way to do it without AWK, using only kubectl and the built-in jsonpath functionality:

kubectl delete replicaset $(kubectl get replicaset -o jsonpath='{ .items[?(@.spec.replicas==0)].metadata.name }')

This should be more stable because it processes the replica count directly instead of trying to parse the output of kubectl which might change.


Removing old replicasets is part of the Deployment object, but it is optional. You can set .spec.revisionHistoryLimit to tell the Deployment how many old replicasets to keep around.

Here is a YAML example:

apiVersion: apps/v1
kind: Deployment
# ...
spec:
  # ...
  revisionHistoryLimit: 0 # Default to 10 if not specified
  # ...

Tags:

Kubernetes