2016-03-31 18:29:39 +02:00
|
|
|
---
|
2020-10-27 01:58:30 +01:00
|
|
|
layout: "docs"
|
2016-03-31 18:29:39 +02:00
|
|
|
page_title: "Command: state rm"
|
2018-12-21 03:18:13 +01:00
|
|
|
sidebar_current: "docs-commands-state-sub-rm"
|
2016-03-31 18:29:39 +02:00
|
|
|
description: |-
|
2021-05-12 00:46:50 +02:00
|
|
|
The `terraform state rm` command removes bindings from the Terraform state, causing Terraform to "forget about" existing objects.
|
2016-03-31 18:29:39 +02:00
|
|
|
---
|
|
|
|
|
|
|
|
# Command: state rm
|
|
|
|
|
2021-05-12 00:46:50 +02:00
|
|
|
The main function of [Terraform state](/docs/language/state/index.html) is
|
|
|
|
to track the bindings between resource instance addresses in your configuration
|
|
|
|
and the remote objects they represent. Normally Terraform automatically
|
|
|
|
updates the state in response to actions taken when applying a plan, such as
|
|
|
|
removing a binding for an remote object that has now been deleted.
|
|
|
|
|
|
|
|
You can use `terraform state rm` in the less common situation where you wish
|
|
|
|
to remove a binding to an existing remote object without first destroying it,
|
|
|
|
which will effectively make Terraform "forget" the object while it continues
|
|
|
|
to exist in the remote system.
|
2016-03-31 18:29:39 +02:00
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
Usage: `terraform state rm [options] ADDRESS...`
|
|
|
|
|
2021-05-12 00:46:50 +02:00
|
|
|
Terraform will search the state for any instances matching the given
|
|
|
|
[resource address](/docs/cli/state/resource-addressing.html), and remove
|
|
|
|
the record of each one so that Terraform will no longer be tracking the
|
|
|
|
corresponding remote objects.
|
2016-03-31 18:29:39 +02:00
|
|
|
|
2021-05-12 00:46:50 +02:00
|
|
|
This means that although the objects will still continue to exist in the
|
|
|
|
remote system, a subsequent
|
|
|
|
[`terraform plan`](../plan.html)
|
|
|
|
will include an action to create a new object for each of the "forgotten"
|
|
|
|
instances. Depending on the constraints imposed by the remote system, creating
|
|
|
|
those objects might fail if their names or other identifiers conflict with
|
|
|
|
the old objects still present.
|
2016-03-31 18:29:39 +02:00
|
|
|
|
2021-05-12 18:05:03 +02:00
|
|
|
This command also accepts the following options:
|
2016-03-31 18:29:39 +02:00
|
|
|
|
2021-05-12 00:46:50 +02:00
|
|
|
* `-dry-run` - Report all of the resource instances that match the given
|
|
|
|
address without actually "forgetting" any of them.
|
2021-05-11 20:37:32 +02:00
|
|
|
|
2021-05-12 18:05:03 +02:00
|
|
|
* `-lock=false` - Don't hold a state lock during the operation. This is
|
|
|
|
dangerous if others might concurrently run commands against the same
|
|
|
|
workspace.
|
|
|
|
|
|
|
|
* `-lock-timeout=DURATION` - Unless locking is disabled with `-lock=false`,
|
|
|
|
instructs Terraform to retry acquiring a lock for a period of time before
|
|
|
|
returning an error. The duration syntax is a number followed by a time
|
|
|
|
unit letter, such as "3s" for three seconds.
|
|
|
|
|
2021-05-11 20:37:32 +02:00
|
|
|
For configurations using
|
|
|
|
[the `remote` backend](/docs/language/settings/backends/remote.html)
|
|
|
|
only, `terraform state rm`
|
|
|
|
also accepts the option
|
|
|
|
[`-ignore-remote-version`](/docs/language/settings/backends/remote.html#command-line-arguments).
|
|
|
|
|
|
|
|
For configurations using
|
|
|
|
[the `local` state rm](/docs/language/settings/backends/local.html) only,
|
|
|
|
`terraform state rm` also accepts the legacy options
|
|
|
|
[`-state`, `-state-out`, and `-backup`](/docs/language/settings/backends/local.html#command-line-arguments).
|
backend: Validate remote backend Terraform version
When using the enhanced remote backend, a subset of all Terraform
operations are supported. Of these, only plan and apply can be executed
on the remote infrastructure (e.g. Terraform Cloud). Other operations
run locally and use the remote backend for state storage.
This causes problems when the local version of Terraform does not match
the configured version from the remote workspace. If the two versions
are incompatible, an `import` or `state mv` operation can cause the
remote workspace to be unusable until a manual fix is applied.
To prevent this from happening accidentally, this commit introduces a
check that the local Terraform version and the configured remote
workspace Terraform version are compatible. This check is skipped for
commands which do not write state, and can also be disabled by the use
of a new command-line flag, `-ignore-remote-version`.
Terraform version compatibility is defined as:
- For all releases before 0.14.0, local must exactly equal remote, as
two different versions cannot share state;
- 0.14.0 to 1.0.x are compatible, as we will not change the state
version number until at least Terraform 1.1.0;
- Versions after 1.1.0 must have the same major and minor versions, as
we will not change the state version number in a patch release.
If the two versions are incompatible, a diagnostic is displayed,
advising that the error can be suppressed with `-ignore-remote-version`.
When this flag is used, the diagnostic is still displayed, but as a
warning instead of an error.
Commands which will not write state can assert this fact by calling the
helper `meta.ignoreRemoteBackendVersionConflict`, which will disable the
checks. Those which can write state should instead call the helper
`meta.remoteBackendVersionCheck`, which will return diagnostics for
display.
In addition to these explicit paths for managing the version check, we
have an implicit check in the remote backend's state manager
initialization method. Both of the above helpers will disable this
check. This fallback is in place to ensure that future code paths which
access state cannot accidentally skip the remote version check.
2020-11-13 22:43:56 +01:00
|
|
|
|
2021-05-12 00:46:50 +02:00
|
|
|
## Example: Remove all Instances of a Resource
|
2016-03-31 18:29:39 +02:00
|
|
|
|
2021-05-12 00:46:50 +02:00
|
|
|
The following example will cause Terraform to "forget" all of the instances
|
|
|
|
of the `packet_device` resource named "worker".
|
2016-03-31 18:29:39 +02:00
|
|
|
|
2019-08-03 01:36:24 +02:00
|
|
|
```shell
|
|
|
|
$ terraform state rm 'packet_device.worker'
|
2016-03-31 18:29:39 +02:00
|
|
|
```
|
|
|
|
|
2021-05-12 00:46:50 +02:00
|
|
|
A resource that doesn't use `count` or `for_each` has only one instance, so
|
|
|
|
this is also the appropriate syntax to select that single instance.
|
|
|
|
|
|
|
|
## Example: Remove all Instances of a Resource in a Module
|
2016-03-31 18:29:39 +02:00
|
|
|
|
2021-05-12 00:46:50 +02:00
|
|
|
To select a resource that you've defined in a child module you must specify
|
|
|
|
the path of that module as part of the resource address:
|
2019-08-03 01:36:24 +02:00
|
|
|
|
|
|
|
```shell
|
2021-05-12 00:46:50 +02:00
|
|
|
$ terraform state rm 'module.foo.packet_device.worker'
|
2019-08-03 01:36:24 +02:00
|
|
|
```
|
|
|
|
|
2021-05-12 00:46:50 +02:00
|
|
|
## Example: Remove all Instances of all Resources in a Module
|
2019-08-03 01:36:24 +02:00
|
|
|
|
2021-05-12 00:46:50 +02:00
|
|
|
The following example will cause Terraform to "forget" all of the instances
|
|
|
|
associated with all resources defined in all instances of the module named
|
|
|
|
`foo`:
|
2016-03-31 18:29:39 +02:00
|
|
|
|
2019-08-03 01:36:24 +02:00
|
|
|
```shell
|
2021-05-12 00:46:50 +02:00
|
|
|
$ terraform state rm 'module.foo'
|
2016-03-31 18:29:39 +02:00
|
|
|
```
|
2019-08-03 01:36:24 +02:00
|
|
|
|
2021-05-12 00:46:50 +02:00
|
|
|
## Example: Remove a Particular Instance of a Resource using `count`
|
2019-08-03 01:36:24 +02:00
|
|
|
|
2021-05-12 00:46:50 +02:00
|
|
|
A resource defined with [the `count` meta-argument](/docs/language/meta-arguments/count.html)
|
|
|
|
has multiple instances that are each identified by an integer. You can
|
|
|
|
select a particular instance by including an explicit index in your given
|
|
|
|
address:
|
2019-08-03 01:36:24 +02:00
|
|
|
|
|
|
|
```shell
|
|
|
|
$ terraform state rm 'packet_device.worker[0]'
|
|
|
|
```
|
|
|
|
|
2021-05-12 00:46:50 +02:00
|
|
|
Brackets (`[`, `]`) have a special meaning in some shells, so you may need to
|
|
|
|
quote or escape the address in order to pass it literally to Terraform.
|
|
|
|
The above shows the typical quoting syntax for Unix-style shells.
|
2019-08-03 01:36:24 +02:00
|
|
|
|
2021-05-12 00:46:50 +02:00
|
|
|
## Example: Remove a Particular Instance of a Resource using `for_each`
|
2019-08-03 01:36:24 +02:00
|
|
|
|
2021-05-12 00:46:50 +02:00
|
|
|
A resource defined with [the `for_each` meta-argument](/docs/language/meta-arguments/for_each.html)
|
|
|
|
has multiple instances that are each identified by an string. You can
|
|
|
|
select a particular instance by including an explicit key in your given
|
|
|
|
address.
|
|
|
|
|
|
|
|
However, the syntax for strings includes quotes and the quote symbol often
|
|
|
|
has special meaning in command shells, so you'll need to use the appropriate
|
|
|
|
quoting and/or escaping syntax for the shell you are using. For example:
|
|
|
|
|
|
|
|
Unix-style shells, such as on Linux or macOS:
|
2019-08-03 01:36:24 +02:00
|
|
|
|
|
|
|
```shell
|
|
|
|
$ terraform state rm 'packet_device.worker["example"]'
|
|
|
|
```
|
|
|
|
|
2021-05-12 00:46:50 +02:00
|
|
|
Windows Command Prompt (`cmd.exe`):
|
2019-08-03 01:36:24 +02:00
|
|
|
|
|
|
|
```shell
|
2021-05-12 00:46:50 +02:00
|
|
|
$ terraform state rm packet_device.worker[\"example\"]
|
2019-08-03 01:36:24 +02:00
|
|
|
```
|
|
|
|
|
2021-05-12 00:46:50 +02:00
|
|
|
PowerShell:
|
2019-08-03 01:36:24 +02:00
|
|
|
|
|
|
|
```shell
|
2021-05-12 00:46:50 +02:00
|
|
|
$ terraform state rm 'packet_device.worker[\"example\"]'
|
2016-03-31 18:29:39 +02:00
|
|
|
```
|