diff --git a/helper/schema/resource.go b/helper/schema/resource.go index 3c8a12e2b..278095c6f 100644 --- a/helper/schema/resource.go +++ b/helper/schema/resource.go @@ -117,7 +117,12 @@ func (r *Resource) Refresh( } err = r.Read(data, meta) - return data.State(), err + state := data.State() + if state.ID == "" { + state = nil + } + + return state, err } // InternalValidate should be called to validate the structure diff --git a/helper/schema/resource_test.go b/helper/schema/resource_test.go index eaad10cb9..6581e711d 100644 --- a/helper/schema/resource_test.go +++ b/helper/schema/resource_test.go @@ -304,3 +304,35 @@ func TestResourceRefresh(t *testing.T) { t.Fatalf("bad: %#v", actual) } } + +func TestResourceRefresh_delete(t *testing.T) { + r := &Resource{ + Schema: map[string]*Schema{ + "foo": &Schema{ + Type: TypeInt, + Optional: true, + }, + }, + } + + r.Read = func(d *ResourceData, m interface{}) error { + d.SetId("") + return nil + } + + s := &terraform.ResourceState{ + ID: "bar", + Attributes: map[string]string{ + "foo": "12", + }, + } + + actual, err := r.Refresh(s, 42) + if err != nil { + t.Fatalf("err: %s", err) + } + + if actual != nil { + t.Fatalf("bad: %#v", actual) + } +}