- The Command Line
- Basic CLI Features
- Command: destroy
Command: destroy
The tofu destroy command is a convenient way to destroy all remote
objects managed by a particular OpenTofu configuration.
While you will typically not want to destroy long-lived objects in a production
environment, OpenTofu is sometimes used to manage ephemeral infrastructure
for development purposes, in which case you can use tofu destroy to
conveniently clean up all of those temporary objects once you are finished
with your work.
Usage​
Usage: tofu destroy [options]
This command is just a convenience alias for the following command:
tofu apply -destroy
For that reason, this command accepts most of the options that
tofu apply accepts, although it does
not accept a plan file argument and forces the selection of the "destroy"
planning mode.
You can also create a speculative destroy plan, to see what the effect of destroying would be, by running the following command:
tofu plan -destroy
This will run tofu plan in destroy mode, showing
you the proposed destroy changes without executing them.
Forgotten Resources​
When you run tofu destroy, OpenTofu will attempt to destroy all resources
managed by the configuration. However, if any resources have the
lifecycle.destroy
meta-argument set to false, those resources will be "forgotten" instead of
destroyed.
When resources are forgotten:
- They are removed from the OpenTofu state file
- The actual infrastructure objects remain intact in your cloud provider or remote system
- The
tofu destroycommand exits with a non-zero status code to indicate that not all resources were fully removed
This exit code behavior might be important for automation and CI/CD pipelines, as it signals that the destroy operation did not complete as a typical destroy would.
The destroy attribute is persisted in the state file, even when resources are removed from the configuration.
Meaning that resources will not be destroyed even when removed from the configuration.
If you want to fully destroy such resources (still in the state), you must first add the resource configuration back and
remove the lifecycle.destroy attribute (or set it to true).
Alternatively, you can add the removed block for the removed resource with lifecycle.destroy = true, which will override the destroy attribute in the state file.