Skip to content

Instantly share code, notes, and snippets.

@pascalwhoop
Created November 20, 2019 14:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pascalwhoop/282d1cc3f6db790d3a544cbd8d00e662 to your computer and use it in GitHub Desktop.
Save pascalwhoop/282d1cc3f6db790d3a544cbd8d00e662 to your computer and use it in GitHub Desktop.
Cleans the Kubernetes API of all pods in a namespace (in our case airflow)
NAMESPACE=airflow
#edit the > 3 < in the jq statement to correspond to the number of days you want to keep pods around for
kubectl delete pod -n $NAMESPACE $(kubectl get pods -n $NAMESPACE -o json | jq -r '.items[] |
select(.status.phase != "Running") |
select(last(.status.containerStatuses)[].state | has("terminated")) |
select((last(.status.containerStatuses)[].state.terminated.finishedAt | fromdate) < (now - 60*60*24*3)) |
.metadata.name')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment