79

I am using terraform to manage IaC on AWS. There was a s3 bucket created by my terraform project and later I moved the s3 bucket terraform to a different project. So I deleted all s3 related code in my project. When I run terraform apply I get an error saying Error: error deleting S3 Bucket (xxxx): BucketNotEmpty: The bucket you tried to delete is not empty

I understand that terraform tries to delete the bucket since I removed the code from there.

I tried to use terraform refresh but got forbidden error: Error: Forbidden: Forbidden status code: 403, request id: 8351F9C3663AF8FB, host id:.

I know I can delete the terraform state file from local but this requires me to import all resources. How can I solve this issue? I am using local state not remote state.

1
  • You can review the resources before remove using dry run : terraform state rm -dry-run <name>
    – RSW
    Jul 20 at 3:23

7 Answers 7

119

You can remove any resource added to your Terraform like this:

  1. List all state: terraform state list

  2. Remove desired resource from state: terraform state rm <name>

45

First you need to remove the resource from the state file by running terraform state rm <resource-id> and then you need to delete the associated configuration from your codebase. After that run init, plan and you will see no changes are required

1
  • 1
    As stated on another answer, you can alternatively use the name instead of the id Nov 21, 2021 at 22:42
23
  1. List states

    $ terraform state list
    
    data.terraform_remote_state.rg
    
    azurerm_subnet.vsubnet
    
    azurerm_virtual_network.vnet
    
  2. Remove selected state.

    $ terraform state rm azurerm_subnet.vsubnet
    
    Removed azurerm_subnet.vsubnet
    
    Successfully removed 1 resource instance(s).
    
7

If you want to remove ALL resources from state file run:

terraform state rm -dry-run $(terraform state list)

Remove -dry-run afterwards for changes to apply.

6

Couple of things i would like to add,

  1. while running the terraform command to remove a resource from the state, make sure you quote(') the resource id appropriately like so,
terraform state rm 'module.s3.module.s3_bucket["my-s3-bucket-name"].aws_s3_bucket.this[0]'  

Looks silly but, I cannot believe, I just wasted the last 10 minutes scratching my head on this.

  1. Terraform state list can often be very long. If you need to remove a group of resources sharing a common name you can use the following bash oneliner,
terraform state rm $(terraform state list | grep production-files)

For example, the command above will remove a aws_s3_bucket, aws_s3_bucket_policy, aws_s3_bucket_cors_configuration and so on.

  1. Utilize the -dry-run flag before performing a potentially destructive operation like this.
terraform state rm 'resource_id' -dry-run

Output:

Acquiring state lock. This may take a few moments...
Would remove module.s3.module.s3_bucket["production-files"].aws_s3_bucket.this[0]
Releasing state lock. This may take a few moments...
1
  • 1
    Super useful hint on quote(').
    – flowAlong
    Nov 9 at 8:24
3

After some searching I found the solution is to use terraform state rm to remove the s3 resource from my local state file.

1
  1. List the resources in the state. terraform state list
  2. Remove the resource from the state. terraform state rm '<resource_name>'
  3. Remove the resource definition from the codebase.
  4. Terraform apply should show no changes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.