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
kubectl delete:- Run it with the same resource file (or resource type) to remove the specified resources from the cluster.
- Example:
- It is explicit and immediate, so it suits cases where the resources should be removed entirely.
- 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:
- This reverts to a known good state instead of only deleting.
kubectl rollout undo:- Built for deployments, it rolls a deployment back to a former revision.
- Example:
- It is the quickest way to restore a previous stable state after a rolling update.
- 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.
| Method | Description | Pros | Cons |
kubectl delete | Deletes specified resources directly from the cluster. | Simple and direct | Complete removal, no state recovery |
| Version control rollbacks | Reverts to a previous state and reapplies configurations. | Maintains historical state | Requires version control |
kubectl rollout undo | Reverts changes for deployments explicitly. | Quick recovery for deployments | Limited to deployments |
| Custom scripts/CI/CD | Automates reversal processes based on conditions | Highly customizable | Complexity 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.
Related reading
- What is the use for CRD status?
- what is the use of external IP address with a ClusterIP service type in kubernetes
- What kubectl command can I use to get events sorted by specific fields and print only specific details of events?
- What major problems would I have if I use Raft Consensus Algorithm in 50+ pods?
- What is the right approach when using STL container for median calculation?
- What is the runtime performance cost of a Docker container?
- what provides the metric container_cpu_usage_seconds_total to prometheus?
- What to do with Released persistent volume?
System design interview problems on this topic
- Design a Container Orchestration System: solution and walkthrough
- Design a Code Deployment System: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a GPU Cluster Scheduler for ML Training: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.