kubectl deleteDelete Kubernetes resources safely
kubectl delete removes objects by name, by label, or by the files that created them. Deleting a pod managed by a Deployment just gets it replaced; deleting the Deployment, a namespace or a PVC is permanent. Know which you are doing, and in which cluster.
kubectl delete (TYPE NAME... | TYPE -l SELECTOR | -f FILE) [-n NS] [--grace-period=N] [--force] [--dry-run=server] [--cascade=orphan]One api replica has a stuck in-memory cache. Replace just that pod; the Deployment will start a fresh one.
kubectlThe Kubernetes CLI.
kubectl sends DELETE /api/v1/namespaces/prod/pods/NAME (or one per object for selectors and files) with a propagation policy. For a pod, the API server doesn't remove it at once: it sets deletionTimestamp and the grace period. The kubelet sees that, runs any preStop hook, sends SIGTERM to the containers, waits up to terminationGracePeriodSeconds (default 30), then SIGKILL, and finally confirms the deletion so the object disappears. Endpoints controllers remove the pod from Services as soon as it's marked. For owners like a Deployment, the garbage collector deletes dependents (ReplicaSets, then pods) using ownerReferences: background by default, foreground or orphan on request. Objects with finalizers (PVCs, namespaces, load-balancer Services) stay Terminating until their controller does its cleanup and removes the finalizer. kubectl waits for all of this by default (--wait=true).
-n NSNamespace to delete in-l SELECTORDelete everything of that type with matching labels-f FILE|DIRDelete exactly the objects defined in these manifests--grace-period=NSeconds between SIGTERM and SIGKILL--force --grace-period=0Remove the API object immediately, don't wait for the kubelet--dry-run=serverShow what would be deleted, delete nothing--cascade=orphanDelete the owner but keep its dependents (e.g. keep pods)--cascade=foregroundDelete dependents first, then the owner--wait=falseDon't wait for finalizers and termination--allEvery object of the type in the namespace (dangerous)-i, --interactiveShow the list and ask for confirmationkubectl delete pods --all -n prod to 'restart the app'Every pod dies at once: a full outage until replacements are Ready.
kubectl rollout restart deploy/api -n prod # rolling, respects maxUnavailablekubectl delete ns staging while your context points at prodWrong context plus a namespace delete removes every object in it, PVCs included, and can't be undone.
kubectl config current-context first; use separate kubeconfigs or a prompt that shows the contextForce-deleting StatefulSet pods by reflexThe old container may still run on a partitioned node; the new pod with the same identity can corrupt shared data.
Confirm the node is really gone (or fence it), then force deleteStripping finalizers to make a namespace go awayThe external resources the finalizer protected (load balancers, volumes, DNS records) are left orphaned and still billed.
Find which controller owns the finalizer, fix or reinstall it, let it clean upTry these on a Linux machine of your own. Each task has a hint and a solution.
Write a command that shows, without deleting anything, which pods labelled job=nightly in namespace batch would be deleted.
Write the command to delete the StatefulSet redis in prod but keep its pods running.
Practise safe deletes in the sandbox: create ~/data/tmp with a few files, preview what rm would remove, then remove it.
In AWS Lab > EKS > your cluster > Run kubectl (lab), deploy the sample app, then delete its Deployment and watch its pods disappear (redeploy it with the button afterwards).
kubectl deleteDelete Kubernetes resources safely
kubectl delete removes objects by name, by label, or by the files that created them. Deleting a pod managed by a Deployment just gets it replaced; deleting the Deployment, a namespace or a PVC is permanent. Know which you are doing, and in which cluster.
kubectl delete (TYPE NAME... | TYPE -l SELECTOR | -f FILE) [-n NS] [--grace-period=N] [--force] [--dry-run=server] [--cascade=orphan]One api replica has a stuck in-memory cache. Replace just that pod; the Deployment will start a fresh one.
kubectlThe Kubernetes CLI.
kubectl sends DELETE /api/v1/namespaces/prod/pods/NAME (or one per object for selectors and files) with a propagation policy. For a pod, the API server doesn't remove it at once: it sets deletionTimestamp and the grace period. The kubelet sees that, runs any preStop hook, sends SIGTERM to the containers, waits up to terminationGracePeriodSeconds (default 30), then SIGKILL, and finally confirms the deletion so the object disappears. Endpoints controllers remove the pod from Services as soon as it's marked. For owners like a Deployment, the garbage collector deletes dependents (ReplicaSets, then pods) using ownerReferences: background by default, foreground or orphan on request. Objects with finalizers (PVCs, namespaces, load-balancer Services) stay Terminating until their controller does its cleanup and removes the finalizer. kubectl waits for all of this by default (--wait=true).
-n NSNamespace to delete in-l SELECTORDelete everything of that type with matching labels-f FILE|DIRDelete exactly the objects defined in these manifests--grace-period=NSeconds between SIGTERM and SIGKILL--force --grace-period=0Remove the API object immediately, don't wait for the kubelet--dry-run=serverShow what would be deleted, delete nothing--cascade=orphanDelete the owner but keep its dependents (e.g. keep pods)--cascade=foregroundDelete dependents first, then the owner--wait=falseDon't wait for finalizers and termination--allEvery object of the type in the namespace (dangerous)-i, --interactiveShow the list and ask for confirmationkubectl delete pods --all -n prod to 'restart the app'Every pod dies at once: a full outage until replacements are Ready.
kubectl rollout restart deploy/api -n prod # rolling, respects maxUnavailablekubectl delete ns staging while your context points at prodWrong context plus a namespace delete removes every object in it, PVCs included, and can't be undone.
kubectl config current-context first; use separate kubeconfigs or a prompt that shows the contextForce-deleting StatefulSet pods by reflexThe old container may still run on a partitioned node; the new pod with the same identity can corrupt shared data.
Confirm the node is really gone (or fence it), then force deleteStripping finalizers to make a namespace go awayThe external resources the finalizer protected (load balancers, volumes, DNS records) are left orphaned and still billed.
Find which controller owns the finalizer, fix or reinstall it, let it clean upTry these on a Linux machine of your own. Each task has a hint and a solution.
Write a command that shows, without deleting anything, which pods labelled job=nightly in namespace batch would be deleted.
Write the command to delete the StatefulSet redis in prod but keep its pods running.
Practise safe deletes in the sandbox: create ~/data/tmp with a few files, preview what rm would remove, then remove it.
In AWS Lab > EKS > your cluster > Run kubectl (lab), deploy the sample app, then delete its Deployment and watch its pods disappear (redeploy it with the button afterwards).