Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove a resource from terraform state?

Tags:

terraform

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.

like image 743
Joey Yi Zhao Avatar asked Apr 18 '20 23:04

Joey Yi Zhao


2 Answers

You can remove any resource added to your Terraform by:-

  1. List All state:- terraform state list

  2. remove state which you want to:- terraform state rm <name>

like image 114
Kumar Pankaj Avatar answered Oct 26 '22 20:10

Kumar Pankaj


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

like image 39
Piyush Mattoo Avatar answered Oct 26 '22 22:10

Piyush Mattoo