Destroying Infrastructure
You'll learn how to safely remove IaC-managed resources and clean up state when infrastructure is no longer needed.
Why Destruction Matters
Just as IaC tools help you build infrastructure consistently, they also help you tear it down cleanly. When a project ends, an environment becomes obsolete, or you need to start fresh, you can't just delete resources manually—that would leave orphaned entries in your state file and create drift. Instead, you use your IaC tool's destroy command to remove everything in a coordinated way.
The Destruction Process
Think of infrastructure destruction like dismantling a building using the original blueprints. Your IaC tool reads the state file to understand what exists, then systematically removes each resource in the correct order (respecting dependencies). For example, it knows to delete a server before removing the network it's attached to.
Most tools offer a preview step—similar to the planning phase—so you can see exactly what will be deleted before confirming. This prevents accidental destruction of critical resources.
Clean State After Destruction
When destruction completes successfully, your state file is either emptied or removed entirely (depending on the tool). This confirms that your IaC tool no longer tracks any resources in that configuration. If you're using remote state, the state file itself might remain (now empty) for historical purposes, but all resource records are gone.
When Not to Destroy Everything
Some resources—like databases with critical data or DNS records—might need to survive even when surrounding infrastructure is torn down. Many IaC tools let you mark specific resources as "protected" to prevent accidental deletion.
Key Takeaway: Destroying infrastructure through IaC tools ensures clean, complete teardown with proper dependency ordering and state cleanup—never manually delete IaC-managed resources.