CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Kubernetes / kubectl delete

kubectl delete

Delete Kubernetes resources safely

Intermediatekubernetescleanuptroubleshooting

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.

Syntaxkubectl delete (TYPE NAME... | TYPE -l SELECTOR | -f FILE) [-n NS] [--grace-period=N] [--force] [--dry-run=server] [--cascade=orphan]

Watch it run

One api replica has a stuck in-memory cache. Replace just that pod; the Deployment will start a fresh one.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 2
kubectl delete pod api-7f9c5d4b6-2xkqp -n prod

kubectl waited while the container got SIGTERM and shut down gracefully. Meanwhile the pod was removed from the Service's endpoints, so no new requests reached it.

Anatomy of a command

$
commandkubectl

The Kubernetes CLI.

How it works

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).

Options you'll actually use

-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 confirmation

Common mistakes

kubectl 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 maxUnavailable
kubectl delete ns staging while your context points at prod

Wrong 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 context
Force-deleting StatefulSet pods by reflex

The 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 delete
Stripping finalizers to make a namespace go away

The 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 up

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Write a command that shows, without deleting anything, which pods labelled job=nightly in namespace batch would be deleted.

  2. 2

    Write the command to delete the StatefulSet redis in prod but keep its pods running.

  3. 3

    Practise safe deletes in the sandbox: create ~/data/tmp with a few files, preview what rm would remove, then remove it.

  4. 4

    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).

Related commands

kubectl applyCreate or update resources from YAML, declarativelykubectl getList Kubernetes resources and their statuskubectl rolloutWatch, undo and restart Deployment rolloutsrmRemove files and directories
Previouskubectl applyNextkubectl scale
Linux / Kubernetes / kubectl delete

kubectl delete

Delete Kubernetes resources safely

Intermediatekubernetescleanuptroubleshooting

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.

Syntaxkubectl delete (TYPE NAME... | TYPE -l SELECTOR | -f FILE) [-n NS] [--grace-period=N] [--force] [--dry-run=server] [--cascade=orphan]

Watch it run

One api replica has a stuck in-memory cache. Replace just that pod; the Deployment will start a fresh one.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 2
kubectl delete pod api-7f9c5d4b6-2xkqp -n prod

kubectl waited while the container got SIGTERM and shut down gracefully. Meanwhile the pod was removed from the Service's endpoints, so no new requests reached it.

Anatomy of a command

$
commandkubectl

The Kubernetes CLI.

How it works

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).

Options you'll actually use

-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 confirmation

Common mistakes

kubectl 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 maxUnavailable
kubectl delete ns staging while your context points at prod

Wrong 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 context
Force-deleting StatefulSet pods by reflex

The 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 delete
Stripping finalizers to make a namespace go away

The 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 up

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Write a command that shows, without deleting anything, which pods labelled job=nightly in namespace batch would be deleted.

  2. 2

    Write the command to delete the StatefulSet redis in prod but keep its pods running.

  3. 3

    Practise safe deletes in the sandbox: create ~/data/tmp with a few files, preview what rm would remove, then remove it.

  4. 4

    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).

Related commands

kubectl applyCreate or update resources from YAML, declarativelykubectl getList Kubernetes resources and their statuskubectl rolloutWatch, undo and restart Deployment rolloutsrmRemove files and directories
Previouskubectl applyNextkubectl scale