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 applyfailed during provisioning. - You manually modified software or configuration on an existing cloud instance outside of OpenTofu.
Example: Auto-Taint on Provisioner Failure
local-exec provisioner tries to write the instance’s public IP to a nonexistent path. When you run:
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
Runtofu plan to see any tainted resources in your state:
2. Forcing Resource Replacement
To manually mark a resource as tainted (without immediately destroying it):3. Undoing a Taint
If you accidentally tainted a resource or decide to keep it:tofu plan will no longer list that resource for replacement.
Summary of Taint Commands
| Command | Description | Example |
|---|---|---|
| tofu apply | Applies changes and auto-taints on failures | $ tofu apply |
| tofu apply-replace RESOURCE | Marks a resource as tainted for next apply | $ tofu apply-replace aws_instance.webserver |
| tofu untaint RESOURCE | Removes the taint flag from a resource | $ tofu untaint aws_instance.webserver |
Using
tofu apply-replace will destroy and recreate resources. Ensure you have appropriate backups or snapshots before proceeding.