Skip to content

Instantly share code, notes, and snippets.

@codebykyle
Created April 24, 2022 09:26
Show Gist options
  • Save codebykyle/1c481352c12264fb7da2011aba11c504 to your computer and use it in GitHub Desktop.
Save codebykyle/1c481352c12264fb7da2011aba11c504 to your computer and use it in GitHub Desktop.
Delete kubernetes namespaces which are not deleting due to finalizers
import requests
import json
namespaces_response = requests.get('http://127.0.0.1:8001/api/v1/namespaces').json()
for namespace_item in namespaces_response['items']:
if namespace_item['status']['phase'] == 'Terminating':
url = 'http://127.0.0.1:8001/api/v1/namespaces/%s' % namespace_item['metadata']['name']
namespace_get_data = requests.get(url).json()
updated_body = namespace_get_data
updated_body['spec'] = {}
updated_body['metadata']['finalizers'] = []
print(updated_body)
requests.put(url + '/finalize', json=updated_body)
requests.delete(url)
response = requests.get(url)
print (response)
@codebykyle
Copy link
Author

You need requests:
pip install requests

You also need to connect to your kubernetes cluster with:
kubectl proxy

Run this script to remove all finalizers on hung namespaces

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