Skip to main content
Zustand provides several patterns for resetting state to its initial value, whether for a single store or multiple stores at once.

Reset a Single Store

The following pattern can be used to reset the state to its initial value:
const useSomeStore = create<State & Actions>()((set, get, store) => ({
  // your code here
  reset: () => {
    set(store.getInitialState())
  },
}))
The getInitialState() method returns the state that was set when the store was first created, making it easy to reset to default values.

Reset Multiple Stores

When you need to reset multiple stores at once (for example, on user logout), you can create a custom create wrapper:
1

Set up the reset registry

Create a set to track all store reset functions:
import type { StateCreator } from 'zustand'
import { create: actualCreate } from 'zustand'

const storeResetFns = new Set<() => void>()

const resetAllStores = () => {
  storeResetFns.forEach((resetFn) => {
    resetFn()
  })
}
2

Create a custom create function

Wrap the original create to register reset functions automatically:
export const create = (<T>() => {
  return (stateCreator: StateCreator<T>) => {
    const store = actualCreate(stateCreator)
    storeResetFns.add(() => {
      store.setState(store.getInitialState(), true)
    })
    return store
  }
}) as typeof actualCreate
3

Use your custom create function

Replace the standard create import with your custom one:
// Instead of: import { create } from 'zustand'
import { create } from './create'

const useUserStore = create((set) => ({
  user: null,
  setUser: (user) => set({ user }),
}))

const useCartStore = create((set) => ({
  items: [],
  addItem: (item) => set((state) => ({ items: [...state.items, item] })),
}))
4

Reset all stores

Call resetAllStores() when needed:
// On logout
function handleLogout() {
  resetAllStores()
  // ... other logout logic
}
When using setState with true as the second parameter, it replaces the entire state rather than merging. This ensures a complete reset.

Live Examples

Basic Reset

See a simple example of resetting a single store

Advanced Reset

Explore resetting multiple stores at once

Build docs developers (and LLMs) love