redux-persist: purge() doesn't work
Hello. Using persistor.purge doesn’t clear any data in my storage.
Here’s the code:
const persistor = getPersistor();
await persistor.purge();
getPersistor() function returns persistor, and I called purge function but still nothing changed in my storage.
I also tried this code as well, but doesn’t worked either:
const persistor = getPersistor();
await persistor.purge();
await persistor.flush();
await persistor.persist();
It seems like purge doesn’t work. Used @5.10.0.
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 11
- Comments: 30
Commits related to this issue
- 4497: Reset React components state when logging out The timeout is courtesy of https://github.com/rt2zz/redux-persist/issues/1015 — committed to reload/ding2 by kasperg 3 years ago
- 4497: Reset React components state when logging out The timeout is courtesy of https://github.com/rt2zz/redux-persist/issues/1015 — committed to reload/ding2 by kasperg 3 years ago
@chiaberry I have multiple reducers implemented with a PURGE case as
that way when I call persistor.purge() its actually setting my state back to
INITIAL_STATEThis method is applicable for those who use Redux Toolkit. The extraReducers method in each slice should contain:
Example
Purge Usage:
in all seriousness, I think I found the fix. You all want me to submit a PR?
I just did
and works great.
I was faced this kind of problem yesterday, and keep finding the solution across Google / Stack Overflow. And below is my solution that works fine now for people who is stilling struggling right now. My problem that I need to get through yesterday is my redux-persist is not working after my rootReducer reset by
return rootReducer(undefined, action);. And here is my solution in my LogoutScreen (i.e. in stack navigation), and mystore.jsno need to change the original code (i.e.return rootReducer(undefined, action);)credit to @Trashpants for the hint, but for those of my fellow Redux Toolkit fans, you need to add an ‘extraReducer’ on your slice.
I was having a similar issue, but after I implemented
perge()I was still having issues. It turned out to be a race condition where some other action would write to state and trigger persisting data that I expected to be cleared. If a user logged out while an API call was loading, it would cause fun issues. I ended up with:@suleymanozev thank you thousand times and more kind sir. Saved me 😃
Lol. Read the docs. It’s a callback, not a promise. Add a timeout and just do the purge after 1 second.
I was confused by this too - not sure if you fixed it by now, but for any people who stumble across this in the future: in order for this to work you have to make sure there is a PURGE action for each reducer that returns your ‘clean’ state. Then your commands of purge(), flush() will clear and force save the changes.
For reference I am coding with react-native (so I assume this is the same for react)
what’s confusing is that if you immediately call purge() after setting up the persistor in store.js it will clear it on boot as expected
import { persistor } from “…/…/…/…/reduxToolkit/store”;
const clearPersistData=()=>{ persistor.pause(); persistor.flush().then(() => { return persistor.purge(); }); }
this worked for me !!! @chungmarcoo thanks for the help
Whe is the customEntityAdapter importing from?
This example is for those who use
react-native. But I’m pretty sure it will work the same forreact.You must reload your application, this is the whole solution. You take the NativeModules from react-native
persistor.purge().then(() => NativeModules.DevSettings.reload());This works great.
I don’t understand most of the comments. persistore.purge() as it’s clearing localStorage has to be a promise / async. You can await it inside an async function and I’d use a try/catch as well:
I assume just doing
persister.purge()without awaiting or without .then(() => /*do something*/)isn’t gonna work. I guess the documentation could have some more examples.works for me!
And I am getting
undefined