Bootstrap FreeKB - OpenShift - Delete namespace using the Python Kubernetes client
OpenShift - Delete namespace using the Python Kubernetes client

Updated:   |  OpenShift articles

The openshift package can be used to interact with OpenShift in Python. pip install can be used to install the OpenShift package.

pip install openshift

 

You may want to first check out my article FreeKB - OpenShift - Login into OpenShift using Python.

And here is an example of how you could delete a project / namespace using the Python Kubernetes client.

from kubernetes import client
from openshift.dynamic import DynamicClient
from openshift.helper.userpassauth import OCPLoginConfiguration

apihost  = 'https://api.openshift.example.com:6443'
username = 'john.doe'
password = 'itsasecret'

kubeConfig = OCPLoginConfiguration(ocp_username=username, ocp_password=password)
kubeConfig.host = apihost
kubeConfig.verify_ssl = False
kubeConfig.get_token()

k8s_client = client.ApiClient(kubeConfig)
dyn_client = DynamicClient(k8s_client)

namespaces = dyn_client.resources.get(api_version='v1', kind='Namespace')
response   = namespaces.delete(name='my-project')

print(response)

 

Something like this should be returned.

ResourceInstance[Namespace]:
  apiVersion: v1
  kind: Namespace
  metadata:
    annotations:
      openshift.io/description: my project
      openshift.io/display-name: my-project
      openshift.io/requester: john.doe
      openshift.io/sa.scc.mcs: s0:c33,c7
      openshift.io/sa.scc.supplemental-groups: 1001070000/10000
      openshift.io/sa.scc.uid-range: 1001070000/10000
    creationTimestamp: '2025-06-24T15:13:31Z'
    deletionTimestamp: '2025-09-25T09:47:35Z'
    labels:
      kubernetes.io/metadata.name: my-project
      pod-security.kubernetes.io/audit: restricted
      pod-security.kubernetes.io/audit-version: v1.24
      pod-security.kubernetes.io/warn: restricted
      pod-security.kubernetes.io/warn-version: v1.24
    managedFields:
    - apiVersion: v1
      fieldsType: FieldsV1
      fieldsV1:
        f:metadata:
          f:annotations:
            .: {}
            f:openshift.io/description: {}
            f:openshift.io/display-name: {}
            f:openshift.io/requester: {}
            f:openshift.io/sa.scc.mcs: {}
            f:openshift.io/sa.scc.supplemental-groups: {}
            f:openshift.io/sa.scc.uid-range: {}
            f:scheduler.alpha.kubernetes.io/defaultTolerations: {}
          f:labels:
            .: {}
            f:kubernetes.io/metadata.name: {}
            f:pod-security.kubernetes.io/audit: {}
            f:pod-security.kubernetes.io/audit-version: {}
            f:pod-security.kubernetes.io/warn: {}
            f:pod-security.kubernetes.io/warn-version: {}
        f:spec:
          f:finalizers: {}
      manager: velero-server
      operation: Update
      time: '2025-06-24T15:13:31Z'
    name: my-project
    resourceVersion: '630499693'
    uid: 6fc4fae3-3e63-4d41-9087-254438241066
  spec:
    finalizers:
    - kubernetes
  status:
    phase: Terminating

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 8d423c in the box below so that we can be sure you are a human.