Skip to content

Instantly share code, notes, and snippets.

@renuka-fernando
Last active January 15, 2023 13:16
Show Gist options
  • Save renuka-fernando/2cf4420c5460cfd965f3b891091af4c2 to your computer and use it in GitHub Desktop.
Save renuka-fernando/2cf4420c5460cfd965f3b891091af4c2 to your computer and use it in GitHub Desktop.
Kubernetes - Force delete a Namespace with Invalid Finalizers
# namespace to be deleted in k8s
NAMESPACE=foo
# Terminal 1
kubectl proxy
# Terminal 2
kubectl get ns $NAMESPACE -o json | \
jq '.spec.finalizers=[]' | \
curl -X PUT "http://localhost:8001/api/v1/namespaces/${NAMESPACE}/finalize" -H "Content-Type: application/json" --data @-
@odselsevier
Copy link

#!/usr/bin/env bash

set -e
set -o pipefail

kubectl proxy &
proxy_pid="$!"
trap 'kill "$proxy_pid"' EXIT

for ns in $(kubectl get namespace --field-selector=status.phase=Terminating --output=jsonpath="{.items[*].metadata.name}"); do
    echo "Removing finalizers from namespace '$ns'..."
    curl -H "Content-Type: application/json" -X PUT "127.0.0.1:8001/api/v1/namespaces/$ns/finalize" -d @- \
        < <(kubectl get namespace "$ns" --output=json | jq '.spec = { "finalizers": [] }')

    echo
    echo "Force-deleting namespace '$ns'..."
    kubectl delete namespace "$ns" --force --grace-period=0 --ignore-not-found=true
done

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