diff --git a/viper.go b/viper.go index 3205b0f..e8ec2a3 100644 --- a/viper.go +++ b/viper.go @@ -1416,9 +1416,16 @@ func (v *Viper) Unset(key string) { path := strings.Split(key, v.keyDelim) lastKey := strings.ToLower(path[len(path)-1]) - deepestMap := deepSearch(v.override, path[0:len(path)-1]) - delete(deepestMap, lastKey) + for _, cfgMap := range []map[string]interface{}{ + v.override, v.config, v.defaults, + v.kvstore, + } { + cfg := deepSearch(cfgMap, path[0:len(path)-1]) + delete(cfg, lastKey) + } + + delete(v.aliases, key) } // ReadInConfig will discover and load the configuration file from disk diff --git a/viper_test.go b/viper_test.go index 45bf8e9..da66d5c 100644 --- a/viper_test.go +++ b/viper_test.go @@ -406,6 +406,17 @@ func TestOverrides(t *testing.T) { assert.Equal(t, 40, Get("age")) } +func TestUnset(t *testing.T) { + SetDefault("unset", 20) + Set("unset", 10) + RegisterAlias("unset_alias", "unset") + + Unset("unset") + + assert.Equal(t, nil, Get("unset")) + assert.Equal(t, nil, Get("unset_alias")) +} + func TestDefaultPost(t *testing.T) { assert.NotEqual(t, "NYC", Get("state")) SetDefault("state", "NYC")