github.com/chanzuckerberg/terraform@v0.11.12-beta1/command/state_rm.go (about) 1 package command 2 3 import ( 4 "context" 5 "fmt" 6 "strings" 7 8 "github.com/hashicorp/terraform/command/clistate" 9 "github.com/mitchellh/cli" 10 ) 11 12 // StateRmCommand is a Command implementation that shows a single resource. 13 type StateRmCommand struct { 14 StateMeta 15 } 16 17 func (c *StateRmCommand) Run(args []string) int { 18 args, err := c.Meta.process(args, true) 19 if err != nil { 20 return 1 21 } 22 23 cmdFlags := c.Meta.flagSet("state show") 24 cmdFlags.StringVar(&c.backupPath, "backup", "-", "backup") 25 cmdFlags.BoolVar(&c.Meta.stateLock, "lock", true, "lock state") 26 cmdFlags.DurationVar(&c.Meta.stateLockTimeout, "lock-timeout", 0, "lock timeout") 27 cmdFlags.StringVar(&c.statePath, "state", "", "path") 28 if err := cmdFlags.Parse(args); err != nil { 29 return cli.RunResultHelp 30 } 31 args = cmdFlags.Args() 32 33 if len(args) < 1 { 34 c.Ui.Error("At least one resource address is required.") 35 return 1 36 } 37 38 state, err := c.State() 39 if err != nil { 40 c.Ui.Error(fmt.Sprintf(errStateLoadingState, err)) 41 return 1 42 } 43 44 if c.stateLock { 45 stateLocker := clistate.NewLocker(context.Background(), c.stateLockTimeout, c.Ui, c.Colorize()) 46 if err := stateLocker.Lock(state, "state-rm"); err != nil { 47 c.Ui.Error(fmt.Sprintf("Error locking state: %s", err)) 48 return 1 49 } 50 defer stateLocker.Unlock(nil) 51 } 52 53 if err := state.RefreshState(); err != nil { 54 c.Ui.Error(fmt.Sprintf("Failed to load state: %s", err)) 55 return 1 56 } 57 58 stateReal := state.State() 59 if stateReal == nil { 60 c.Ui.Error(fmt.Sprintf(errStateNotFound)) 61 return 1 62 } 63 64 if err := stateReal.Remove(args...); err != nil { 65 c.Ui.Error(fmt.Sprintf(errStateRm, err)) 66 return 1 67 } 68 69 c.Ui.Output(fmt.Sprintf("%d items removed.", len(args))) 70 71 if err := state.WriteState(stateReal); err != nil { 72 c.Ui.Error(fmt.Sprintf(errStateRmPersist, err)) 73 return 1 74 } 75 76 if err := state.PersistState(); err != nil { 77 c.Ui.Error(fmt.Sprintf(errStateRmPersist, err)) 78 return 1 79 } 80 81 c.Ui.Output("Item removal successful.") 82 return 0 83 } 84 85 func (c *StateRmCommand) Help() string { 86 helpText := ` 87 Usage: terraform state rm [options] ADDRESS... 88 89 Remove one or more items from the Terraform state. 90 91 This command removes one or more items from the Terraform state based 92 on the address given. You can view and list the available resources 93 with "terraform state list". 94 95 This command creates a timestamped backup of the state on every invocation. 96 This can't be disabled. Due to the destructive nature of this command, 97 the backup is ensured by Terraform for safety reasons. 98 99 Options: 100 101 -backup=PATH Path where Terraform should write the backup 102 state. This can't be disabled. If not set, Terraform 103 will write it to the same path as the statefile with 104 a backup extension. 105 106 -lock=true Lock the state file when locking is supported. 107 108 -lock-timeout=0s Duration to retry a state lock. 109 110 -state=PATH Path to the source state file. Defaults to the configured 111 backend, or "terraform.tfstate" 112 113 ` 114 return strings.TrimSpace(helpText) 115 } 116 117 func (c *StateRmCommand) Synopsis() string { 118 return "Remove an item from the state" 119 } 120 121 const errStateRm = `Error removing items from the state: %s 122 123 The state was not saved. No items were removed from the persisted 124 state. No backup was created since no modification occurred. Please 125 resolve the issue above and try again.` 126 127 const errStateRmPersist = `Error saving the state: %s 128 129 The state was not saved. No items were removed from the persisted 130 state. No backup was created since no modification occurred. Please 131 resolve the issue above and try again.`