terraform destroy
is a command used remove all resources defined in Terraform files. It is a critical feature that allows developers and system administrators to deprovision infrastructure that is no longer required, ensuring that they do not continue to incur costs for resources they are not using.
When you run terraform destroy
, Terraform reads your configuration files, determines the dependency order for destroying the resources, and then makes the appropriate API calls to the cloud provider to tear down the resources. It's vital to be cautious when using this command because it does not discriminate which resources it terminates; it will destroy all resources managed by Terraform in your configuration.
terraform destroy
terraform plan -destroy
to review what resources will be destroyed before actually running terraform destroy
.terraform destroy
with the -target
option.terraform destroy
to avoid accidentally removing critical infrastructure.By understanding and carefully implementing terraform destroy
, you can effectively manage the lifecycle of your cloud infrastructure in a cost-effective and efficient manner.
terraform destroy
Here’s a sample output detailing what you might see when you execute this command. The example provided assumes you have a configuration set up to create resources on AWS that includes an EC2 instance and an S3 bucket, as previously discussed.
$ terraform destroy
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
- destroy
Terraform will perform the following actions:
# aws_instance.example will be destroyed
- resource "aws_instance" "example" {
- ami = "ami-0c55b159cbfafe1f0"
- arn = "arn:aws:ec2:us-west-2:123456789012:instance/i-1234567890abcdef0"
- associate_public_ip_address = true
- availability_zone = "us-west-2a"
- id = "i-1234567890abcdef0"
- instance_state = "running"
- instance_type = "t2.micro"
- key_name = "my-key"
- private_ip = "192.168.1.2"
- public_ip = "54.183.22.255"
- security_groups = ["default"]
- tags = {
- "Name" = "MyInstance"
}
- tenancy = "default"
...
}
# aws_s3_bucket.b will be destroyed
- resource "aws_s3_bucket" "b" {
- acl = "private"
- arn = "arn:aws:s3:::my-tf-test-bucket"
- bucket = "my-tf-test-bucket"
- force_destroy = false
- id = "my-tf-test-bucket"
- region = "us-west-2"
- tags = {
- "Environment" = "Dev"
}
...
}
Plan: 0 to add, 0 to change, 2 to destroy.
Do you really want to destroy all resources in your configuration?
Terraform will destroy all your managed infrastructure, as shown above.
There is no undo. Only 'yes' will be accepted to confirm.
Enter a value:
In this output:
destroy
symbol (-
) next to each resource that will be removed.yes
to proceed.If you confirm by typing yes
, Terraform will proceed to destroy the resources, and you will see logs of each action it takes, followed by a confirmation message once all resources have been destroyed:
aws_instance.example: Destroying... [id=i-1234567890abcdef0]
aws_instance.example: Destruction complete after 1m
aws_s3_bucket.b: Destroying... [id=my-tf-test-bucket]
aws_s3_bucket.b: Destruction complete after 10s
Destroy complete! Resources: 2 destroyed.
This output provides a clear, step-by-step account of what Terraform does when terraform destroy
is executed, helping users understand the impact of their command before and after execution. Always make sure to review the resources listed for destruction to prevent any unintended loss of service.