2014-07-28 19:43:00 +02:00
|
|
|
---
|
|
|
|
layout: "intro"
|
|
|
|
page_title: "Destroy Infrastructure"
|
|
|
|
sidebar_current: "gettingstarted-destroy"
|
|
|
|
---
|
|
|
|
|
|
|
|
# Destroy Infrastructure
|
|
|
|
|
|
|
|
We've now seen how to build and change infrastructure. Before we
|
|
|
|
move on to creating multiple resources and showing resource
|
|
|
|
dependencies, we're going to go over how to completely destroy
|
|
|
|
the Terraform-managed infrastructure.
|
|
|
|
|
|
|
|
Destroying your infrastructure is a rare event in production
|
|
|
|
environments. But if you're using Terraform to spin up multiple
|
|
|
|
environments such as development, test, QA environments, then
|
|
|
|
destroying is a useful action.
|
|
|
|
|
|
|
|
## Plan
|
|
|
|
|
2014-10-13 20:57:43 +02:00
|
|
|
Before destroying our infrastructure, we can use the plan command
|
|
|
|
to see what resources Terraform will destroy.
|
2014-07-28 19:43:00 +02:00
|
|
|
|
|
|
|
```
|
2014-10-13 20:57:43 +02:00
|
|
|
$ terraform plan -destroy
|
2014-07-28 19:43:00 +02:00
|
|
|
...
|
|
|
|
|
|
|
|
- aws_instance.example
|
|
|
|
```
|
|
|
|
|
2014-10-13 20:57:43 +02:00
|
|
|
With the `-destroy` flag, we're asking Terraform to plan a destroy,
|
|
|
|
where all resources under Terraform management are destroyed. You can
|
|
|
|
use this output to verify exactly what resources Terraform is managing
|
|
|
|
and will destroy.
|
2014-07-28 19:43:00 +02:00
|
|
|
|
2014-10-13 20:57:43 +02:00
|
|
|
## Destroy
|
2014-07-28 19:43:00 +02:00
|
|
|
|
2014-10-13 20:57:43 +02:00
|
|
|
Let's destroy the infrastructure now:
|
2014-07-28 19:43:00 +02:00
|
|
|
|
|
|
|
```
|
2014-10-13 20:57:43 +02:00
|
|
|
$ terraform destroy
|
2014-07-28 19:43:00 +02:00
|
|
|
aws_instance.example: Destroying...
|
|
|
|
|
|
|
|
Apply complete! Resources: 0 added, 0 changed, 1 destroyed.
|
|
|
|
|
|
|
|
...
|
|
|
|
```
|
|
|
|
|
2014-10-13 20:57:43 +02:00
|
|
|
The `terraform destroy` command should ask you to verify that you
|
|
|
|
really want to destroy the infrastructure. Terraform only accepts the
|
|
|
|
literal "yes" as an answer as a safety mechanism. Once entered, Terraform
|
|
|
|
will go through and destroy the infrastructure.
|
2014-07-28 19:43:00 +02:00
|
|
|
|
2014-10-13 20:57:43 +02:00
|
|
|
Just like with `apply`, Terraform is smart enough to determine what order
|
|
|
|
things should be destroyed. In our case, we only had one resource, so there
|
|
|
|
wasn't any ordering necessary. But in more complicated cases with multiple
|
|
|
|
resources, Terraform will destroy in the proper order.
|
2014-07-28 19:43:00 +02:00
|
|
|
|
|
|
|
## Next
|
|
|
|
|
|
|
|
You now know how to create, modify, and destroy infrastructure.
|
|
|
|
With these building blocks, you can effectively experiment with
|
|
|
|
any part of Terraform.
|
|
|
|
|
|
|
|
Next, we move on to features that make Terraform configurations
|
|
|
|
slightly more useful: [variables, resource dependencies, provisioning,
|
|
|
|
and more](/intro/getting-started/dependencies.html).
|