Skip to content

Instantly share code, notes, and snippets.

@jossef
Created August 13, 2020 12:50
Show Gist options
  • Save jossef/a563f8651ec52ad03a243dec539b333d to your computer and use it in GitHub Desktop.
Save jossef/a563f8651ec52ad03a243dec539b333d to your computer and use it in GitHub Desktop.
force delete k8s namespace
#!/usr/bin/env python3
import atexit
import json
import requests
import subprocess
import sys
namespace = sys.argv[1]
proxy_process = subprocess.Popen(['kubectl', 'proxy'])
atexit.register(proxy_process.kill)
p = subprocess.Popen(['kubectl', 'get', 'namespace', namespace, '-o', 'json'], stdout=subprocess.PIPE)
p.wait()
data = json.load(p.stdout)
data['spec']['finalizers'] = []
requests.put('http://127.0.0.1:8001/api/v1/namespaces/{}/finalize'.format(namespace), json=data).raise_for_status()
@leonnf
Copy link

leonnf commented Jun 18, 2022

Thanks for sharing, It's worked

@hkmehandiratta
Copy link

Worked well. Thank You.

@pavelzag
Copy link

Very cool, thank you!

@42atomys
Copy link

42atomys commented Nov 3, 2022

little sugar if you need to force delete all "Terminating" namespace at once

kubectl get ns | grep Terminating | awk '{print $1}' | xargs -I '{}' python3 force-delete-k8s-namespace.py {}

@achintyx2
Copy link

Thanks it worked!

@marcelomrwin
Copy link

wow! really works. Thanks.

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