Destroy infrastructure
You have now created and updated an VCN and subnet on Oracle Cloud Infrastructure (OCI) with Terraform. In this tutorial, you will use Terraform to destroy this infrastructure.
Once you no longer need infrastructure you might want to destroy it to reduce your security exposure and ongoing costs. For example you may remove a production environment from service, or manage short-lived environments like build or test systems. In addition to building and modifying infrastructure, Terraform can destroy or recreate the infrastructure it manages.
Prerequisites
This tutorial assumes that you are continuing from the previous tutorial, which we highly recommend. If not, we've summarized the prerequisites here for your convenience. To follow this tutorial you will need:
OCI Tenancy. Note your region, you will use it throughout the tutorial.
The Terraform CLI installed.
The OCI CLI installed. Configure the CLI with a token by following the session authentication flow.
Create a directory named
learn-terraform-oci
and paste the following configuration into a file namedmain.tf
.Customize the
region
and both instances ofcompartment_id
to match the ones listed on your OCI tenancy page in the web console.Initialize the configuration.
Apply the configuration. Respond to the confirmation prompt with a
yes
.
Once you have successfully applied the configuration, you can continue with the rest of this tutorial.
Destroy
The terraform destroy
command terminates resources defined in your Terraform
configuration. This command is the reverse of terraform apply
in that it
terminates all the resources specified by the configuration. It does not
destroy resources running elsewhere that are not described in the current
configuration.
Destroy your infrastructure.
The -
prefix indicates that the subnet and VCN will be destroyed. As with apply,
Terraform shows its execution plan and waits for approval before making any
changes.
Answer yes
to execute this plan and destroy the infrastructure.
Just like with apply
, Terraform determines the order in which your resources
must be destroyed. In this case, Terraform destroyed the subnet, and then the VCN. In more complicated cases with
multiple resources, Terraform will destroy them in a suitable order to respect
dependencies.
Next steps
You have destroyed your infrastructure on OCI. Learn more about the destroy command in the Terraform docs, or continue to the next tutorial to continue learning the basics of Terraform with the OCI provider.