How to Remove OpenTofu Resources with tofu destroy

tofu destroy removes everything OpenTofu created for a project, and nothing else. This guide shows how to remove OpenTofu resources safely: preview what will go, run tofu destroy, and know what stays behind.

It’s a companion to Part 2 of the OpenTofu Tutorial, which ends with tofu destroy.

Cost: free. Everything here runs on your own computer.

Every command and output on this page is real, run on OpenTofu 1.13 on macOS. The commands are the same in Windows PowerShell and on Linux.

Prerequisites

resource "local_file" "home_page" {
  filename = "index.html"
  content  = "<h1>Bookshop</h1>"
}

resource "local_file" "about_page" {
  filename = "about.html"
  content  = "<h1>About the Bookshop</h1>"
}

Step 1: Preview What Will Be Removed

Before deleting anything, ask OpenTofu what a destroy would do. This changes nothing:

tofu plan -destroy
local_file.home_page: Refreshing state... [id=c0b605bc039c5f97ef612cd50f8e285306aea74f]
local_file.about_page: Refreshing state... [id=37072a8b03ae68359fb9608d01f10e68800dedc0]

OpenTofu used the selected providers to generate the following execution plan.
Resource actions are indicated with the following symbols:
  - destroy

OpenTofu will perform the following actions:

  # local_file.about_page will be destroyed
  - resource "local_file" "about_page" {
      - content              = "<h1>About the Bookshop</h1>" -> null
      - content_base64sha256 = "AtnpxxT0SXkYHAAmx8q1H5fdR91vlTgElaSACvdrJJg=" -> null
      - content_base64sha512 = "6IpY/ggFpkKX1emhDe1Ernw3aQp3koTWIfWwcmu9dBx9xX/EPFdt05/gKfhBO6ZdP9SZ7jUCvfKR38c4PH/Wgw==" -> null
      - content_md5          = "34d1e0e98e3886e127a5e573d18f1007" -> null
      - content_sha1         = "37072a8b03ae68359fb9608d01f10e68800dedc0" -> null
      - content_sha256       = "02d9e9c714f44979181c0026c7cab51f97dd47dd6f95380495a4800af76b2498" -> null
      - content_sha512       = "e88a58fe0805a64297d5e9a10ded44ae7c37690a779284d621f5b0726bbd741c7dc57fc43c576dd39fe029f8413ba65d3fd499ee3502bdf291dfc7383c7fd683" -> null
      - directory_permission = "0777" -> null
      - file_permission      = "0777" -> null
      - filename             = "about.html" -> null
      - id                   = "37072a8b03ae68359fb9608d01f10e68800dedc0" -> null
    }

  # local_file.home_page will be destroyed
  - resource "local_file" "home_page" {
      - content              = "<h1>Bookshop</h1>" -> null
      - content_base64sha256 = "+5sTgVHJocmrr02gbEGM4VmZc6Dc2n0A0CHFa1Kr2js=" -> null
      - content_base64sha512 = "geSAoyg3EZpsjVqNqLLJOy38aTEed/kD6zxHGHWSx/sbGZgWxo2NPAuI+sdB3FGlL2UQHlLzH5cZ99r3gqaZQA==" -> null
      - content_md5          = "d9c651a380dd05647dd51dd41330682d" -> null
      - content_sha1         = "c0b605bc039c5f97ef612cd50f8e285306aea74f" -> null
      - content_sha256       = "fb9b138151c9a1c9abaf4da06c418ce1599973a0dcda7d00d021c56b52abda3b" -> null
      - content_sha512       = "81e480a32837119a6c8d5a8da8b2c93b2dfc69311e77f903eb3c47187592c7fb1b199816c68d8d3c0b88fac741dc51a52f65101e52f31f9719f7daf782a69940" -> null
      - directory_permission = "0777" -> null
      - file_permission      = "0777" -> null
      - filename             = "index.html" -> null
      - id                   = "c0b605bc039c5f97ef612cd50f8e285306aea74f" -> null
    }

Plan: 0 to add, 0 to change, 2 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.

Every block starts with -, which means destroy, and the summary says 2 to destroy. If the list holds anything you want to keep, stop here.

Step 2: Run tofu destroy

tofu destroy

It prints the same plan as Step 1, then asks before it deletes anything. Type yes. Here’s the output from the summary line on:

A Terminal window at the end of tofu destroy: the plan says 2 to destroy, OpenTofu asks Do you really want to destroy all resources and warns there is no undo, the answer is yes, and it reports Destroy complete! Resources: 2 destroyed.
tofu destroy shows what it will remove and waits for yes.
Plan: 0 to add, 0 to change, 2 to destroy.

Do you really want to destroy all resources?
  OpenTofu will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes

local_file.about_page: Destroying... [id=37072a8b03ae68359fb9608d01f10e68800dedc0]
local_file.home_page: Destroying... [id=c0b605bc039c5f97ef612cd50f8e285306aea74f]
local_file.about_page: Destruction complete after 0s
local_file.home_page: Destruction complete after 0s

Destroy complete! Resources: 2 destroyed.

Read the warning: There is no undo. Only the full word yes goes ahead; anything else cancels. Destroy complete! means both pages are gone.

What Stays Behind

  • Your configuration. main.tf is untouched, so tofu apply can build everything again.
  • The state files. terraform.tfstate now lists no resources, and terraform.tfstate.backup holds the state from before the destroy.
  • Anything OpenTofu didn’t create. tofu destroy only removes what its state records, so other files in the folder are safe.

Common Mistakes

MistakeWhat happensDo this instead
Running it in the wrong folderOpenTofu destroys whatever that folder’s state manages.Check the folder, then run tofu plan -destroy first.
Using tofu destroy to remove one resourceEverything in the project goes, not just that one.Delete that resource’s block from your .tf file and run tofu apply.
Deleting the folder insteadAnything OpenTofu created outside the folder, such as containers or cloud resources, keeps running, and OpenTofu no longer knows about it.Run tofu destroy first, then delete the folder.

FAQ

Can I undo tofu destroy?

No, as the prompt warns. You can run tofu apply to create the resources again, but anything stored inside them is gone.

Is tofu destroy the same as tofu apply -destroy?

Yes. OpenTofu’s documentation calls tofu destroy a convenience alias for tofu apply -destroy, which is why it accepts most of the same options.

How do I skip the yes prompt?

Add -auto-approve, but only in automation. tofu apply -auto-approve: Apply Without the Prompt explains when that’s safe.

Conclusion

Preview with tofu plan -destroy, then run tofu destroy and type yes. It removes only what OpenTofu created for that project, keeps your configuration, and can’t be undone, so read the list before you confirm.

Related

Additional Resources

OpenTofu® is a registered trademark of LF Projects, LLC. Other names mentioned are trademarks of their respective owners. JD Bots isn’t affiliated with or endorsed by the OpenTofu project.

Leave a Reply

Up ↑