Skip to content

Instantly share code, notes, and snippets.

@StevenACoffman
Forked from ltupin/sh
Last active September 17, 2024 16:44
Show Gist options
  • Save StevenACoffman/b51cdeabc49abf66f45a171a76e04858 to your computer and use it in GitHub Desktop.
Save StevenACoffman/b51cdeabc49abf66f45a171a76e04858 to your computer and use it in GitHub Desktop.
Filter failed kubernetes jobs to delete it
#Should be a job too :-D
# With xargs (on all namespaces)
kc get jobs -o=jsonpath='{range .items[?(@.status.conditions[0].type == "Failed")]}{.metadata.name}{"\t"}{.metadata.namespace}{"\n"}{end}' --all-namespaces | \
xargs -n2 sh -c 'kubectl delete jobs $0 --namespace=$1'
# For loop (only in the current namespace)
for i in $(kc get jobs -o=jsonpath='{range .items[?(@.status.conditions[0].type == "Failed")]}{.metadata.name}{"\n"}{end}');
do kubectl delete jobs $i; done
# Delete jobs whose age is measured in days (could still be actively running!)
export NAMESPACE='demographics'
kubectl get job -n $NAMESPACE | awk 'match($4,/[0-9]+d/) {print $1}' | xargs kubectl delete job -n $NAMESPACE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment