github.com/pulumi/terraform@v1.4.0/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 the [Terraform Cloud CLI integration](/cli/cloud) or the [`remote` backend](/language/settings/backends/remote) 53 only, `terraform state rm` 54 also accepts the option 55 [`-ignore-remote-version`](/cli/cloud/command-line-arguments#ignore-remote-version). 56 57 For configurations using 58 [the `local` state rm](/language/settings/backends/local) only, 59 `terraform state rm` also accepts the legacy options 60 [`-state`, `-state-out`, and `-backup`](/language/settings/backends/local#command-line-arguments). 61 62 ## Example: Remove all Instances of a Resource 63 64 The following example will cause Terraform to "forget" all of the instances 65 of the `packet_device` resource named "worker". 66 67 ```shell 68 $ terraform state rm 'packet_device.worker' 69 ``` 70 71 A resource that doesn't use `count` or `for_each` has only one instance, so 72 this is also the appropriate syntax to select that single instance. 73 74 ## Example: Remove all Instances of a Resource in a Module 75 76 To select a resource that you've defined in a child module you must specify 77 the path of that module as part of the resource address: 78 79 ```shell 80 $ terraform state rm 'module.foo.packet_device.worker' 81 ``` 82 83 ## Example: Remove all Instances of all Resources in a Module 84 85 The following example will cause Terraform to "forget" all of the instances 86 associated with all resources defined in all instances of the module named 87 `foo`: 88 89 ```shell 90 $ terraform state rm 'module.foo' 91 ``` 92 93 ## Example: Remove a Particular Instance of a Resource using `count` 94 95 A resource defined with [the `count` meta-argument](/language/meta-arguments/count) 96 has multiple instances that are each identified by an integer. You can 97 select a particular instance by including an explicit index in your given 98 address: 99 100 ```shell 101 $ terraform state rm 'packet_device.worker[0]' 102 ``` 103 104 Brackets (`[`, `]`) have a special meaning in some shells, so you may need to 105 quote or escape the address in order to pass it literally to Terraform. 106 The above shows the typical quoting syntax for Unix-style shells. 107 108 ## Example: Remove a Particular Instance of a Resource using `for_each` 109 110 A resource defined with [the `for_each` meta-argument](/language/meta-arguments/for_each) 111 has multiple instances that are each identified by an string. You can 112 select a particular instance by including an explicit key in your given 113 address. 114 115 However, the syntax for strings includes quotes and the quote symbol often 116 has special meaning in command shells, so you'll need to use the appropriate 117 quoting and/or escaping syntax for the shell you are using. For example: 118 119 Unix-style shells, such as on Linux or macOS: 120 121 ```shell 122 $ terraform state rm 'packet_device.worker["example"]' 123 ``` 124 125 Windows Command Prompt (`cmd.exe`): 126 127 ```shell 128 $ terraform state rm packet_device.worker[\"example\"] 129 ``` 130 131 PowerShell: 132 133 ```shell 134 $ terraform state rm 'packet_device.worker[\"example\"]' 135 ```