github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/website/docs/commands/state/rm.html.md (about)

     1  ---
     2  layout: "commands-state"
     3  page_title: "Command: state rm"
     4  sidebar_current: "docs-commands-state-sub-rm"
     5  description: |-
     6    The `terraform state rm` command removes items from the Terraform state.
     7  ---
     8  
     9  # Command: state rm
    10  
    11  The `terraform state rm` command is used to remove items from the
    12  [Terraform state](/docs/state/index.html). This command can remove
    13  single resources, single instances of a resource, entire modules,
    14  and more.
    15  
    16  ## Usage
    17  
    18  Usage: `terraform state rm [options] ADDRESS...`
    19  
    20  Remove one or more items from the Terraform state.
    21  
    22  Items removed from the Terraform state are _not physically destroyed_.
    23  Items removed from the Terraform state are only no longer managed by
    24  Terraform. For example, if you remove an AWS instance from the state, the AWS
    25  instance will continue running, but `terraform plan` will no longer see that
    26  instance.
    27  
    28  There are various use cases for removing items from a Terraform state
    29  file. The most common is refactoring a configuration to no longer manage
    30  that resource (perhaps moving it to another Terraform configuration/state).
    31  
    32  The state will only be saved on successful removal of all addresses.
    33  If any specific address errors for any reason (such as a syntax error),
    34  the state will not be modified at all.
    35  
    36  This command will output a backup copy of the state prior to saving any
    37  changes. The backup cannot be disabled. Due to the destructive nature
    38  of this command, backups are required.
    39  
    40  This command requires one or more addresses that point to a resources in the
    41  state. Addresses are
    42  in [resource addressing format](/docs/commands/state/addressing.html).
    43  
    44  The command-line flags are all optional. The list of available flags are:
    45  
    46  * `-backup=path` - Path where Terraform should write the backup state. This
    47    can't be disabled. If not set, Terraform will write it to the same path as
    48    the statefile with a backup extension.
    49  
    50  * `-state=path` - Path to a Terraform state file to use to look up
    51    Terraform-managed resources. By default it will use the configured backend,
    52    or the default "terraform.tfstate" if it exists.
    53  
    54  ## Example: Remove a Resource
    55  
    56  The example below removes the `packet_device` resource named `worker`:
    57  
    58  ```shell
    59  $ terraform state rm 'packet_device.worker'
    60  ```
    61  
    62  ## Example: Remove a Module
    63  
    64  The example below removes the entire module named `foo`:
    65  
    66  ```shell
    67  $ terraform state rm 'module.foo'
    68  ```
    69  
    70  ## Example: Remove a Module Resource
    71  
    72  The example below removes the `packet_device` resource named `worker` inside a module named `foo`:
    73  
    74  ```shell
    75  $ terraform state rm 'module.foo.packet_device.worker'
    76  ```
    77  
    78  ## Example: Remove a Resource configured with count
    79  
    80  The example below removes the first instance of a `packet_device` resource named `worker` configured with
    81  [`count`](/docs/configuration/resources.html#count-multiple-resource-instances-by-count):
    82  
    83  ```shell
    84  $ terraform state rm 'packet_device.worker[0]'
    85  ```
    86  
    87  ## Example: Remove a Resource configured with for_each
    88  
    89  The example below removes the `"example"` instance of a `packet_device` resource named `worker` configured with
    90  [`for_each`](/docs/configuration/resources.html#for_each-multiple-resource-instances-defined-by-a-map-or-set-of-strings):
    91  
    92  Linux, Mac OS, and UNIX:
    93  
    94  ```shell
    95  $ terraform state rm 'packet_device.worker["example"]'
    96  ```
    97  
    98  PowerShell:
    99  
   100  ```shell
   101  $ terraform state rm 'packet_device.worker[\"example\"]'
   102  ```
   103  
   104  Windows `cmd.exe`:
   105  
   106  ```shell
   107  $ terraform state rm packet_device.worker[\"example\"]
   108  ```