Skip to content

Instantly share code, notes, and snippets.

@ekristen
Created January 30, 2020 19:06
Show Gist options
  • Save ekristen/0d10a85013a828bb30c7287288caa8fa to your computer and use it in GitHub Desktop.
Save ekristen/0d10a85013a828bb30c7287288caa8fa to your computer and use it in GitHub Desktop.
Shell One Liner to Remove Bad Finalizer From Kubernetes Resources
# Swap pod for any resource you need to iterate over. (ie daemonset, deployment)
kubectl get pods --all-namespaces | tail -n+2 | awk '{print $1 " " $2}' | xargs -L1 bash -c "kubectl patch -n \$0 pod/\$1 --type=merge -p \$(kubectl get -n \$0 pod/\$1 -o json | jq -Mcr '.metadata.finalizers // [] | {metadata:{finalizers:map(select(. != \"name-of-bad-finalizer\"))}}')"
@dulek
Copy link

dulek commented Dec 22, 2022

If you want to be safe from race conditions and lost updates, you got to use kubectl replace and pass whole resource so that resourceVersion is checked. Something like this for a single-resource version:

kubectl get -n $ns $res $name -o json | jq -Mcr "if .metadata.finalizers != null then del(.metadata.finalizers[] | select(. == \"name-of-bad-finalizer\")) else . end" | kubectl replace -n $ns $res $name -f -

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment