Delete depends on data item that doesn't exist

I’m trying to delete some old workspaces and the resources contained within them.

I’m running into an issue where a resource that was created from a different project, and referenced via a data item in my project. The definition for this data item is:

data "aws_iam_role" "s3_read_role" {
  name = "load_from_s3_role-${var.identifier}"
}

When I try to run “terraform destroy”, I get an error about not being able to find that data item.

I tried using “terraform state rm” to remove that data item from the state resource list, but I still get the error. I’m assuming because Terraform is parsing the .tf files and looking for any data items mentioned.

Is there a way to say “delete resources in workspace without looking up data items”?

Or better yet, is there a way to delete resources in a workspace without parsing the tf files? For example: for each item in (terraform state list); run terraform destroy ${item}; done.

Thank you for any help you’re able to provide!

-Anthony

Hi @aucm,

Unfortunately there isn’t a way to skip reading data resources during planning because often typically other parts of the configuration depend on the results of the data resources, and so Terraform’s design assumes you’d want to use the latest information about those objects.

Perhaps a different way to get your intended result would be to edit the configuration to delete everything except any terraform blocks (configuring backend and provider dependencies) and any provider blocks (configuring providers), and then run terraform apply. Terraform should then see that you’ve removed everything and so plan to delete the objects still tracked in the state.

If your provider configurations depend on any results from data resources then you’ll need to either retain those data blocks in particular or rewrite those provider configurations to use hard-coded values instead.

Unfortunately terraform destroy ends up being quite different than just deleting everything from the configuration as I described above, because for terraform destroy Terraform must still evaluate all of the indirect values (local values, input variables, output values) and configure providers in the configuration. Whereas if you literally delete the objects in the configuration then Terraform can tell that nothing is depending on them anymore, and so the planning operation will focus only on detecting the objects from the state that are no longer present in the configuration.

Thanks for the reply, I’ll look into that “empty except terraform and provider block” method. I really appreciate you taking the time to answer in a detailed and straightforward way!