LinuxCommandLibrary

kubectl-delete

Delete Kubernetes resources

TLDR

Delete a specific pod

$ kubectl delete [[po|pod]] [pod_name]
copy

Delete a specific deployment
$ kubectl delete [[deploy|deployment]] [deployment_name]
copy

Delete a specific node
$ kubectl delete [[no|node]] [node_name]
copy

Delete all pods in a specified namespace
$ kubectl delete [[po|pods]] --all [[-n|--namespace]] [namespace]
copy

Delete all deployments and services in a specified namespace
$ kubectl delete [[deploy|deployment]],[[svc|services]] --all [[-n|--namespace]] [namespace]
copy

Delete all nodes
$ kubectl delete [[no|nodes]] --all
copy

Delete resources defined in a YAML manifest
$ kubectl delete [[-f|--filename]] [path/to/manifest.yaml]
copy

SYNOPSIS

kubectl delete RESOURCE_TYPE NAME [OPTIONS]
kubectl delete -f FILE_OR_DIR [OPTIONS]
kubectl delete -k KUSTOMIZATION_DIR [OPTIONS]
kubectl delete --all [OPTIONS]
kubectl delete --all-namespaces [OPTIONS]

PARAMETERS

RESOURCE_TYPE
    The type of Kubernetes resource to delete (e.g., pod, deployment, service, namespace).

NAME
    The name of the specific resource to delete.

-f, --filename stringArray
    Path to a file or directory containing the configuration of resources to delete. Supports YAML and JSON.

-k, --kustomize string
    Path to a Kustomization directory containing manifests to delete.

--all
    Delete all resources of the specified type in the current or specified namespace.

--all-namespaces
    Operate on resources across all namespaces. Often used with --all or for cluster-scoped resources.

--cascade boolean
    If true, cascade the deletion of dependent resources. Default is true.

--force
    Force immediate deletion without a graceful shutdown period. Use with caution.

--grace-period int
    Period in seconds to wait before forcing the deletion of pods. Default is server-defined (-1). Set to 0 for immediate termination with --force.

--dry-run string
    If client, only print the object that would be sent without sending it. If server, apply dry run on the server. If none, do nothing. Useful for testing.

--timeout duration
    The maximum time to wait for a delete operation to complete (e.g., 5s, 1m). Default is 0 (no timeout).

-l, --selector string
    Selector (label query) to filter resources for deletion (e.g., app=my-app).

--field-selector string
    Selector (field query) to filter resources for deletion (e.g., status.phase=Succeeded).

-n, --namespace string
    If present, specifies the namespace scope for the CLI request. Only applicable to namespaced resources.

--wait
    If true, wait for the resources to be deleted before returning. Default is true.

--ignore-not-found
    Treat 'resource not found' errors as successful deletions instead of failures.

DESCRIPTION

The `kubectl delete` command is used to remove Kubernetes resources from a cluster. It is a fundamental operation for managing the lifecycle of applications and infrastructure within Kubernetes. Resources can be deleted by specifying their type and name, using label selectors to target multiple resources, or by providing a configuration file (YAML or JSON) that defines the resources to be deleted. This command supports options for controlling deletion behavior, such as forcing deletion, setting grace periods for graceful termination, and dry-running operations to preview changes without committing them. `kubectl delete` is essential for cleaning up environments, removing misconfigured deployments, or deprovisioning services. Caution should be exercised as deletions are generally irreversible.

CAVEATS

Deletion via `kubectl delete` is generally irreversible. By default, it performs a cascading deletion, meaning dependent resources (e.g., Pods of a Deployment) are also removed. Use the --force and --grace-period=0 flags with extreme caution as they can lead to data loss or corruption for stateful applications by bypassing graceful termination. Deleting a Kubernetes namespace can take a significant amount of time, as all resources within that namespace must be fully terminated first. Be mindful of resource dependencies; deleting a resource that others rely on without proper coordination can cause issues.

BEST PRACTICES

Always confirm the resources you intend to delete using kubectl get or kubectl describe before initiating a deletion. For critical operations, utilize --dry-run=client to preview the impact of the command. Exercise extreme caution when using options like --all or --all-namespaces, as they can have wide-ranging, irreversible effects across your cluster. Understand the implications of cascading deletions and how they affect dependent resources.

COMMON ERROR HANDLING

A common error is 'NotFound' when the specified resource does not exist; the --ignore-not-found flag can suppress this. Ensure you have the necessary RBAC permissions (Role-Based Access Control) to delete resources; insufficient permissions will result in 'Forbidden' errors.

HISTORY

The `kubectl delete` command has been an integral part of the Kubernetes command-line interface since its early development. Its evolution has mirrored the growing maturity of Kubernetes, with additions like improved --dry-run capabilities (client-side vs. server-side), more nuanced control over cascading deletions via --cascade, and explicit termination controls with --force and --grace-period. These enhancements have aimed to provide users with safer, more precise, and more robust ways to manage the lifecycle of resources within a Kubernetes cluster, reflecting the increasing complexity and demands of cloud-native deployments.

SEE ALSO

kubectl apply(1), kubectl create(1), kubectl get(1), kubectl edit(1), kubectl describe(1), kubectl patch(1)

Copied to clipboard