Skip to content

Disposing Stores ​

In application code, you should never need to dispose a store. Stores are meant to live as long as the app. Disposing is mostly useful in tests and in apps that create multiple pinia instances, where leaving a store or a pinia instance behind can cause memory leaks.

$dispose() ​

store.$dispose() stops the watchers created in the store, removes its $subscribe() and $onAction() subscriptions, and removes it from the pinia instance. It doesn't reset or delete its state: the next call to useStore() creates a new store that reuses the previous state.

js
const store = useCounterStore()
store.count = 10
store.$dispose()

const newStore = useCounterStore() // a new store instance
newStore.count // 10

To start from the initial state instead, also delete the state from the pinia instance:

js
const counter = useCounterStore()
counter.$dispose()
delete pinia.state.value[counter.$id]

DANGER

A store that is being used should not be disposed of. This is just the wrong usage of $dispose() and maybe you should rethink the store to have a collection that allows disposing of items instead of the store itself.

disposePinia() ​

disposePinia(pinia) clears the pinia instance (plugins, subscriptions, state) to allow garbage collection. it can't be used afterwards.

Released under the MIT License.