Kubernetes
kubectl
reverse operation
kubectl delete
container management

What is the reverse of kubectl apply?

Codemia Knowledge Hub ·

The direct reverse of kubectl apply -f config.yaml is kubectl delete -f config.yaml, which removes the resources defined in that file. If you want to go back to a previous state rather than remove everything, reapply an older version of the file from version control, or use kubectl rollout undo for a deployment.

kubectl apply is declarative: you give it a YAML or JSON file describing the desired state of your resources, and it creates or updates them in the cluster to match. Undoing it therefore means either deleting those resources or declaring an earlier state.

Ways to undo kubectl apply

  1. kubectl delete:
    • Run it with the same resource file (or resource type) to remove the specified resources from the cluster.
    • Example:
bash
     kubectl delete -f config.yaml
  • It is explicit and immediate, so it suits cases where the resources should be removed entirely.
  1. Reapplying an earlier version from version control:
    • If the configuration files are in version control, check out a previous version and apply it again.
    • Example:
bash
     git checkout <previous-commit>
     kubectl apply -f config.yaml
  • This reverts to a known good state instead of only deleting.
  1. kubectl rollout undo:
    • Built for deployments, it rolls a deployment back to a former revision.
    • Example:
bash
     kubectl rollout undo deployment/<deployment-name>
  • It is the quickest way to restore a previous stable state after a rolling update.
  1. Custom scripts and CI/CD pipelines:
    • Scripts can track applied changes and revert them when needed.
    • Pipelines can trigger revert jobs on build failures, using the techniques above or custom logic.
MethodDescriptionProsCons
kubectl deleteDeletes specified resources directly from the cluster.Simple and directComplete removal, no state recovery
Version control rollbacksReverts to a previous state and reapplies configurations.Maintains historical stateRequires version control
kubectl rollout undoReverts changes for deployments explicitly.Quick recovery for deploymentsLimited to deployments
Custom scripts/CI/CDAutomates reversal processes based on conditionsHighly customizableComplexity and potential configuration drift

Risks when reverting

  • Stateful resources: Persistent Volumes and databases need care, because deleting and recreating them can lose data.
  • Configuration drift: differences between the cluster state and the files can cause unexpected behavior when you reapply or delete.
  • Dependencies: resources that depend on each other may need careful ordering so you do not remove something critical by accident.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design