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()
@jossef
Copy link
Author

jossef commented Aug 13, 2020

usage:

python3 force-delete-k8s-namespace.py my-bad-namespace

@rjshree
Copy link

rjshree commented Sep 9, 2020

Worked like a charm. Thanks jossef

@wioxjk
Copy link

wioxjk commented Jun 30, 2021

Much love,
thank you!

@dn4n
Copy link

dn4n commented Jul 6, 2021

Yeah, it works like a charm!

@canriquezds
Copy link

It worked on my end. Thanks 👍🏼

@ernest-kr
Copy link

worked well Thanks

@akanshgupta
Copy link

That worked, thanks

@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.

@Atxx737
Copy link

Atxx737 commented Apr 21, 2025

Thanks! It's very helping me

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