apify/crawlee · error · Error

Cannot clear the state persisted under key '${this.#persistS

Error message

Cannot clear the state persisted under key '${this.#persistStateKey}' while it is still being persisted periodically - the next PERSIST_STATE event would write it straight back. Use reset() to reset the state itself, or teardown() before clearing the record.

What it means

resetStore() deletes the persisted KeyValueStore record for the state, but throws if the instance is still listening for PERSIST_STATE events, because the next periodic persist would immediately rewrite the record. It instructs you to reset the in-memory state with reset() or stop persistence with teardown() first.

Source

Thrown at packages/core/src/recoverable_state.ts:288

     * Use {@apilink RecoverableState.resetStore} to clear the persisted record as well.
     */
    reset(): void {
        this.#state = this.#defaultState();
    }

    /**
     * Clear the persisted state record, leaving the in-memory state alone.
     *
     * This is a between-lifecycles operation - its point is to stop the next {@apilink RecoverableState.initialize}
     * from restoring the record, so it throws while PERSIST_STATE events are still being handled, where the next
     * one would write the record straight back. Use {@apilink RecoverableState.reset} to reset the state itself,
     * or {@apilink RecoverableState.teardown} before clearing the record.
     *
     * A no-op if persistence is disabled or no KeyValueStore is available yet.
     */
    async resetStore(): Promise<void> {
        if (this.#listening) {
            throw new Error(
                `Cannot clear the state persisted under key '${this.#persistStateKey}' while it is still being persisted periodically - the next PERSIST_STATE event would write it straight back. Use reset() to reset the state itself, or teardown() before clearing the record.`,
            );
        }

        if (!this.#persistenceEnabled) {
            return;
        }

        const keyValueStore = await this.#resolveKeyValueStore();

        if (keyValueStore === null) {
            return;
        }

        await this.#withTimeout(
            async () => keyValueStore.setValue(this.#persistStateKey, null),
            'Clearing the persisted state',
        );

View on GitHub (pinned to dbe57fb09c)

Solutions

  1. Call await instance.teardown() before resetStore()
  2. Use instance.reset() instead if you only want to reset the state itself, not delete the record
  3. Check persistence is intentionally enabled; if not needed, construct without persistence

Example fix

// before
await recoverableState.resetStore();
// after
await recoverableState.teardown();
await recoverableState.resetStore();
Defensive patterns

Strategy: try-catch

Validate before calling

if (state.isPersisting /* listening */) await state.teardown();
await state.resetStore();

Try / catch

try {
  await state.resetStore();
} catch (err) {
  if (err.message.includes('still being persisted')) {
    await state.teardown();
    await state.resetStore();
  } else throw err;
}

Prevention

When it happens

Trigger: Calling await recoverableState.resetStore() while the instance is actively persisted (listening), i.e. before teardown().

Common situations: Cleanup code that clears storage without tearing down the state manager; tests clearing the store between runs while the state is still live; trying to wipe user data without disabling persistence.

Related errors


AI-assisted analysis of apify/crawlee@dbe57fb09c (2026-08-30). Data as JSON: /api/errors/dd7200e81da25414. Report an issue: GitHub.