Can we destroy/remove all resources deployed via module

Hi there,

I’m posting this question to know if I can destroy/remove all the resources created by a module. Here is the catch, I am calling that module on a counter, so multiple resources are created.

module1 = 10 resources
module2 = 10 resources
module3 = 10 resources

Now I want to delete module2, is there a simple way to delete all the 10 resources created under module2?

Another similar question is can we delete/remove resources with counter?


can I delete resource for something similar to:
# this doesn't work in terraform
terraform destroy -target aws_instance.fruit[1]

I was going through these links:


Hi @Let-itGo,

The usual way to destroy something in Terraform is to remove it from your configuration and run terraform apply.

Terraform is a “desired state” system, and so it will notice that the objects it previously created are no longer in the configuration (which describes the “desired state”) and so propose to delete them to make the remote system match your configuration change.

1 Like

Hi @apparentlymart

I too face similar issue to the op. I have defined a module (“db”) that creates multiple resources (14 resources) while provisioning a databese. I then invoke the module twice from my with say “my_db1” and “my_db2”. The providers are passed into these invocations. Now i want to delete all the resources created for my_db1. To do this, i simply removed the block
module "my_db1" { source= "..\modules\db" ... ... providers { .... } }
I then run terraform plan. I was expecting to see that terraform would be destroying 14 resources that where created as part of “my_db1”. Instead i get error about “Provider configuration not present”

What am i doing wrong? Please let me know if you need any details.

Kind Regards

Hi @mbapai,

It sounds like you are using a module which contains a provider block, and so removing the module is removing both the resources in the module and the provider configuration that would be required to destroy them, making it impossible for Terraform to proceed.

Including provider blocks in non-root modules is not recommended specifically because it creates this situation, but Terraform unfortunately must continue to allow it for backward compatibility.

If you are able to modify the child module then the best solution would be to redesign it so that it does not declare its own provider configurations and instead uses only provider configurations defined in the root module. If you complete that refactoring before you remove the module then Terraform will be able to use the provider configuration in the root module to destroy the resources in the child module.

If you cannot modify the child module then there is a different approach to trick Terraform:

  • Make a new module directory containing only a single .tf file containing a provider configuration equivalent to the one in your existing module.
  • Find the module block you want to remove and instead change its source argument to refer to the new module directory you created in the previous step.
  • Run terraform init to re-initialize with the new module source location.
  • Run terraform apply to apply the change. Terraform should notice that the child module still exists but no longer contains any resources, and so it will propose to delete the existing objects belonging to that module.
  • One the apply is complete and all of the objects have been destroyed, you can then safely remove the module block because there will no longer be any existing objects depending on the provider configuration inside.

Unless you can modify the real module to not have its own provider configuration, you will need to repeat this process every time you remove a call to this module.