Search code examples
terraform

How can I remove a resource from terraform state?


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.


Solution

  • 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