Skip to main content
OpenTofu’s taint mechanism lets you mark resources for destruction and recreation, ensuring failed or stale instances are replaced automatically. While the legacy tofu taint command is deprecated, its core logic remains intact under the new apply-replace flag.

What Is a Tainted Resource?

A tainted resource in OpenTofu is one you explicitly mark (or is marked automatically) for replacement on the next apply. This is useful when:
  • A previous tofu apply failed during provisioning.
  • You manually modified software or configuration on an existing cloud instance outside of OpenTofu.

Example: Auto-Taint on Provisioner Failure

Here, a local-exec provisioner tries to write the instance’s public IP to a nonexistent path. When you run:
the creation fails and OpenTofu marks the resource as tainted.
A tainted resource will be destroyed and recreated on the next tofu apply. This behavior mirrors terraform taint in Terraform CLI.

1. Detecting a Tainted Resource

Run tofu plan to see any tainted resources in your state:
You’ll see output similar to:
Even if the EC2 instance still exists in AWS, OpenTofu will destroy and recreate it because it’s marked tainted.

2. Forcing Resource Replacement

To manually mark a resource as tainted (without immediately destroying it):
Inspect the planned replacement:
Output:

3. Undoing a Taint

If you accidentally tainted a resource or decide to keep it:
A subsequent tofu plan will no longer list that resource for replacement.

Summary of Taint Commands

Using tofu apply-replace will destroy and recreate resources. Ensure you have appropriate backups or snapshots before proceeding.

Watch Video