This tutorial teaches how to update and destroy infrastructure managed by OpenTofu using a local_file example.
In this tutorial, you’ll learn how to update and destroy infrastructure managed by OpenTofu using a simple local_file example. We’ll walk through modifying resource configuration, previewing changes, applying updates, tearing down resources, and organizing your .tf files for maintainability.
local_file.pet: Refreshing state... [id=cba595b7d9f94ba11074a6f3f731912d95fb3d2c]OpenTofu used the selected providers to generate the following execution plan.Resource actions are indicated with these symbols: -/+ destroy and then create replacement # local_file.pet must be replaced -/+ resource "local_file" "pet" { ~ file_permission = "0777" -> "0700" # forces replacement ~ id = "cba595b7d9f94ba11074a6f3f731912d95fb3d2c" -> (known after apply) ... (other attributes hidden) }Plan: 1 to add, 0 to change, 1 to destroy.Note: You didn't use the `-out` option to save this plan, so OpenTofu can’t guarantee to take exactly these actions if you run `tofu apply` now.
Save your plan with tofu plan -out=plan.tfplan to lock in the exact changes for later application.
You’ll see the same plan, then a confirmation prompt:
Copy
Ask AI
Do you want to perform these actions? # local_file.pet must be replacedEnter a value: yeslocal_file.pet: Destroying... [id=...]local_file.pet: Creation complete after 0sApply complete! Resources: 1 added, 0 changed, 1 destroyed.
When you no longer need the local_file resource, use:
Copy
Ask AI
tofu destroy
Example output:
Copy
Ask AI
local_file.pet: Refreshing state... [id=...]OpenTofu used the selected providers to generate the following execution plan.Resource actions are indicated with: - destroy # local_file.pet will be destroyed - resource "local_file" "pet" { - content = "We love pets!" -> null - file_permission = "0700" -> null - filename = "/root/pets.txt" -> null - id = "..." -> null ... (other attributes hidden) }Plan: 0 to add, 0 to change, 1 to destroy.Do you really want to destroy all resources?Enter a value: yeslocal_file.pet: Destroying... [id=...]local_file.pet: Destruction complete after 0sDestroy complete! Resources: 1 destroyed.
Adding -auto-approve skips all confirmation prompts. Use with caution, as it will immediately destroy your managed resources.
OpenTofu loads all .tf files in the working directory. Splitting configurations into multiple files can enhance readability and maintainability.For example, a directory with two resources:
Copy
Ask AI
[opentofu-local-file]$ ls /root/opentofu-local-filelocal.tf cat.tf