diff --git a/remote/remote.go b/remote/remote.go index 6de522a9c5..14895a31d0 100644 --- a/remote/remote.go +++ b/remote/remote.go @@ -350,6 +350,27 @@ func PushState(conf *terraform.RemoteState, force bool) (StateChangeResult, erro } } +// DeleteState is used to delete the remote state given +// the configuration for the remote endpoint. +func DeleteState(conf *terraform.RemoteState) error { + if conf == nil { + return fmt.Errorf("Missing remote server configuration") + } + + // Setup the client + client, err := NewClientByState(conf) + if err != nil { + return fmt.Errorf("Failed to create remote client: %v", err) + } + + // Destroy the state + err = client.DeleteState() + if err != nil { + return fmt.Errorf("Failed to delete remote state: %v", err) + } + return nil +} + // blankState is used to return a serialized form of a blank state // with only the remote info. func blankState(conf *terraform.RemoteState) ([]byte, error) { diff --git a/remote/remote_test.go b/remote/remote_test.go index 87846c97dd..3b617fe62d 100644 --- a/remote/remote_test.go +++ b/remote/remote_test.go @@ -287,6 +287,21 @@ func TestPushState_Error(t *testing.T) { } } +func TestDeleteState(t *testing.T) { + defer testFixCwd(testDir(t)) + + remote, srv := testRemotePush(t, 200) + defer srv.Close() + + local := terraform.NewState() + testWriteLocal(t, local) + + err := DeleteState(remote) + if err != nil { + t.Fatalf("err: %v", err) + } +} + func TestBlankState(t *testing.T) { remote := &terraform.RemoteState{ Type: "http",