rt2zz / redux-persist

persist and rehydrate a redux store
MIT License
12.91k stars 862 forks source link

Clear storage entry after removing persistence for the entire slice #1383

Open naresh1021 opened 2 years ago

naresh1021 commented 2 years ago

I had persistence applied to some (2) states in my slice and later remove one state from being persisted.

const persistedKeys = ['state1', 'state2'];
const persistConfig = {
  key: 'sample',
  storage: storage,
  whitelist: persistedKeys,
};
const sampleReducers = persistReducer(persistConfig, sampleSlice.reducer);

The persisted value was written correctly to the localstorage.

{state1: "false", state2: "false", _persist: "{"version":-1,"rehydrated":true}"}
state1: "false"
state2: "false"
_persist: "{\"version\":-1,\"rehydrated\":true}"

Later, I removed persistence for one of the states and that entry was removed from the localstorage.

Code: 
const persistedKeys = ['state1'];

Localstorage:
{state1: "false", _persist: "{"version":-1,"rehydrated":true}"}
state1: "false"
_persist: "{\"version\":-1,\"rehydrated\":true}"

Now if I remove the persistence for the last state, the entry for that state is removed from the localstorage but the localstorage now has an empty entry like below.

Code: 
const persistedKeys = [''];

Localstorage:
{_persist: "{"version":-1,"rehydrated":true}"}
_persist: "{\"version\":-1,\"rehydrated\":true}"

I even tried removing the persistReducer applied on the slice's reducer but the empty entry in localstorage still remains.

const sampleReducers = sampleSlice.reducer;

Is it possible to clear this entry as well? Am I missing something?