github.com/hugorut/terraform@v1.1.3/website/docs/cli/commands/state/rm.mdx (about) 1 --- 2 page_title: 'Command: state rm' 3 description: >- 4 The `terraform state rm` command removes bindings from the Terraform state, 5 causing Terraform to "forget about" existing objects. 6 --- 7 8 # Command: state rm 9 10 The main function of [Terraform state](/language/state) is 11 to track the bindings between resource instance addresses in your configuration 12 and the remote objects they represent. Normally Terraform automatically 13 updates the state in response to actions taken when applying a plan, such as 14 removing a binding for a remote object that has now been deleted. 15 16 You can use `terraform state rm` in the less common situation where you wish 17 to remove a binding to an existing remote object without first destroying it, 18 which will effectively make Terraform "forget" the object while it continues 19 to exist in the remote system. 20 21 ## Usage 22 23 Usage: `terraform state rm [options] ADDRESS...` 24 25 Terraform will search the state for any instances matching the given 26 [resource address](/cli/state/resource-addressing), and remove 27 the record of each one so that Terraform will no longer be tracking the 28 corresponding remote objects. 29 30 This means that although the objects will still continue to exist in the 31 remote system, a subsequent 32 [`terraform plan`](/cli/commands/plan) 33 will include an action to create a new object for each of the "forgotten" 34 instances. Depending on the constraints imposed by the remote system, creating 35 those objects might fail if their names or other identifiers conflict with 36 the old objects still present. 37 38 This command also accepts the following options: 39 40 * `-dry-run` - Report all of the resource instances that match the given 41 address without actually "forgetting" any of them. 42 43 * `-lock=false` - Don't hold a state lock during the operation. This is 44 dangerous if others might concurrently run commands against the same 45 workspace. 46 47 * `-lock-timeout=DURATION` - Unless locking is disabled with `-lock=false`, 48 instructs Terraform to retry acquiring a lock for a period of time before 49 returning an error. The duration syntax is a number followed by a time 50 unit letter, such as "3s" for three seconds. 51 52 For configurations using 53 [the `remote` backend](/language/settings/backends/remote) 54 only, `terraform state rm` 55 also accepts the option 56 [`-ignore-remote-version`](/language/settings/backends/remote#command-line-arguments). 57 58 For configurations using 59 [the `local` state rm](/language/settings/backends/local) only, 60 `terraform state rm` also accepts the legacy options 61 [`-state`, `-state-out`, and `-backup`](/language/settings/backends/local#command-line-arguments). 62 63 ## Example: Remove all Instances of a Resource 64 65 The following example will cause Terraform to "forget" all of the instances 66 of the `packet_device` resource named "worker". 67 68 ```shell 69 $ terraform state rm 'packet_device.worker' 70 ``` 71 72 A resource that doesn't use `count` or `for_each` has only one instance, so 73 this is also the appropriate syntax to select that single instance. 74 75 ## Example: Remove all Instances of a Resource in a Module 76 77 To select a resource that you've defined in a child module you must specify 78 the path of that module as part of the resource address: 79 80 ```shell 81 $ terraform state rm 'module.foo.packet_device.worker' 82 ``` 83 84 ## Example: Remove all Instances of all Resources in a Module 85 86 The following example will cause Terraform to "forget" all of the instances 87 associated with all resources defined in all instances of the module named 88 `foo`: 89 90 ```shell 91 $ terraform state rm 'module.foo' 92 ``` 93 94 ## Example: Remove a Particular Instance of a Resource using `count` 95 96 A resource defined with [the `count` meta-argument](/language/meta-arguments/count) 97 has multiple instances that are each identified by an integer. You can 98 select a particular instance by including an explicit index in your given 99 address: 100 101 ```shell 102 $ terraform state rm 'packet_device.worker[0]' 103 ``` 104 105 Brackets (`[`, `]`) have a special meaning in some shells, so you may need to 106 quote or escape the address in order to pass it literally to Terraform. 107 The above shows the typical quoting syntax for Unix-style shells. 108 109 ## Example: Remove a Particular Instance of a Resource using `for_each` 110 111 A resource defined with [the `for_each` meta-argument](/language/meta-arguments/for_each) 112 has multiple instances that are each identified by an string. You can 113 select a particular instance by including an explicit key in your given 114 address. 115 116 However, the syntax for strings includes quotes and the quote symbol often 117 has special meaning in command shells, so you'll need to use the appropriate 118 quoting and/or escaping syntax for the shell you are using. For example: 119 120 Unix-style shells, such as on Linux or macOS: 121 122 ```shell 123 $ terraform state rm 'packet_device.worker["example"]' 124 ``` 125 126 Windows Command Prompt (`cmd.exe`): 127 128 ```shell 129 $ terraform state rm packet_device.worker[\"example\"] 130 ``` 131 132 PowerShell: 133 134 ```shell 135 $ terraform state rm 'packet_device.worker[\"example\"]' 136 ```