Deletion
This section provides details about how to delete resources. It’s important to note that there is a difference between removing a resource and truly deleting it. Deleting a resource with a gracePeriodSeconds
value of 0
will result in its immediate removal from the cluster. However, the resource may still exist in the etcd store until all dependent resources are cleaned up.
The orphanDependents
parameter determines what happens to dependent resources when a resource is deleted. If orphanDependents
is set to True
, dependent resources will be orphaned and may exist as “dead” objects. However, if orphanDependents
is set to False
, dependent resources will be deleted as well. Note that this parameter is deprecated in favor of the propagationPolicy
parameter.
The propagationPolicy
parameter allows you to specify how dependent resources should be deleted. This parameter can be set to one of the following values:
- Orphan: The dependent resources will be orphaned.
- Background: The dependent resources will be deleted in the background. This is the default value.
- Foreground: The dependent resources will be deleted in the foreground.
Here are some examples of how to delete resources using the Kubernetes client:
Deleting a node:
from kubernetes.client import V1DeleteOptions
from kubernetes.client.api import CoreV1Api
api_instance = CoreV1Api()
# Delete the node with the name 'my-node'
api_instance.delete_node(
name='my-node',
body=V1DeleteOptions(grace_period_seconds=5, propagation_policy='Foreground')
)
Deleting a pod:
from kubernetes.client import V1DeleteOptions
from kubernetes.client.api import CoreV1Api
api_instance = CoreV1Api()
# Delete the pod with the name 'my-pod'
api_instance.delete_namespaced_pod(
name='my-pod',
namespace='my-namespace',
body=V1DeleteOptions(grace_period_seconds=10, propagation_policy='Orphan')
)
Deleting a deployment:
from kubernetes.client import V1DeleteOptions
from kubernetes.client.api import AppsV1Api
api_instance = AppsV1Api()
# Delete the deployment with the name 'my-deployment'
api_instance.delete_namespaced_deployment(
name='my-deployment',
namespace='my-namespace',
body=V1DeleteOptions(grace_period_seconds=0, propagation_policy='Background')
)
Deleting a service:
from kubernetes.client import V1DeleteOptions
from kubernetes.client.api import CoreV1Api
api_instance = CoreV1Api()
# Delete the service with the name 'my-service'
api_instance.delete_namespaced_service(
name='my-service',
namespace='my-namespace',
body=V1DeleteOptions(grace_period_seconds=30, propagation_policy='Foreground')
)
For a complete list of available resources and their deletion methods, please refer to the Kubernetes client documentation: https://github.com/kubernetes-client/python/
Top-Level Directory Explanations
doc/ - This directory contains documentation files for the project.
doc/source/ - This directory contains the source files for the documentation.
examples/ - This directory contains example usage of the Kubernetes client library.
examples/dynamic-client/ - This directory contains examples of using the dynamic client to interact with Kubernetes.
kubernetes/ - This directory contains the main Kubernetes client library.
kubernetes/base/ - This directory contains the base Kubernetes client library.
kubernetes/base/config/ - This directory contains configuration files for the base library.
kubernetes/base/dynamic/ - This directory contains the dynamic client implementation for the base library.
kubernetes/base/hack/ - This directory contains hack files for the base library.
kubernetes/base/leaderelection/ - This directory contains the leader election implementation for the base library.
kubernetes/base/watch/ - This directory contains the watch implementation for the base library.
kubernetes/client/ - This directory contains the top-level client for the Kubernetes client library.
kubernetes/client/api/ - This directory contains the API definitions for the client library.
kubernetes/client/models/ - This directory contains the data models used by the client library.
kubernetes/e2e_test/ - This directory contains end-to-end tests for the Kubernetes client library.
scripts/ - This directory contains scripts used in the development and build process.
scripts/util/ - This directory contains utility scripts.