{"record":{"id":"dd7200e81da25414","repo":"apify/crawlee","slug":"cannot-clear-the-state-persisted-under-key-this","errorCode":null,"errorMessage":"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.","messagePattern":"Cannot clear the state persisted under key '(.+?)' 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\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/core/src/recoverable_state.ts","lineNumber":288,"sourceCode":"     * Use {@apilink RecoverableState.resetStore} to clear the persisted record as well.\n     */\n    reset(): void {\n        this.#state = this.#defaultState();\n    }\n\n    /**\n     * Clear the persisted state record, leaving the in-memory state alone.\n     *\n     * This is a between-lifecycles operation - its point is to stop the next {@apilink RecoverableState.initialize}\n     * from restoring the record, so it throws while PERSIST_STATE events are still being handled, where the next\n     * one would write the record straight back. Use {@apilink RecoverableState.reset} to reset the state itself,\n     * or {@apilink RecoverableState.teardown} before clearing the record.\n     *\n     * A no-op if persistence is disabled or no KeyValueStore is available yet.\n     */\n    async resetStore(): Promise<void> {\n        if (this.#listening) {\n            throw new Error(\n                `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.`,\n            );\n        }\n\n        if (!this.#persistenceEnabled) {\n            return;\n        }\n\n        const keyValueStore = await this.#resolveKeyValueStore();\n\n        if (keyValueStore === null) {\n            return;\n        }\n\n        await this.#withTimeout(\n            async () => keyValueStore.setValue(this.#persistStateKey, null),\n            'Clearing the persisted state',\n        );","sourceCodeStart":270,"sourceCodeEnd":306,"githubUrl":"https://github.com/apify/crawlee/blob/dbe57fb09ca607ad59dcf998f3925ef9ac3bb26c/packages/core/src/recoverable_state.ts#L270-L306","documentation":"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.","triggerScenarios":"Calling await recoverableState.resetStore() while the instance is actively persisted (listening), i.e. before teardown().","commonSituations":"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.","solutions":["Call await instance.teardown() before resetStore()","Use instance.reset() instead if you only want to reset the state itself, not delete the record","Check persistence is intentionally enabled; if not needed, construct without persistence"],"exampleFix":"// before\nawait recoverableState.resetStore();\n// after\nawait recoverableState.teardown();\nawait recoverableState.resetStore();","handlingStrategy":"try-catch","validationCode":"if (state.isPersisting /* listening */) await state.teardown();\nawait state.resetStore();","typeGuard":null,"tryCatchPattern":"try {\n  await state.resetStore();\n} catch (err) {\n  if (err.message.includes('still being persisted')) {\n    await state.teardown();\n    await state.resetStore();\n  } else throw err;\n}","preventionTips":["Always tear down RecoverableState instances before clearing their store records","Centralize cleanup in one teardown helper","In tests, reset store only after stopping persistence"],"tags":["state","persistence","lifecycle"],"backgroundTag":"store-clear-while-persisting","analyzedSha":"dbe57fb09ca607ad59dcf998f3925ef9ac3bb26c","analyzedAt":"2026-08-30T22:22:28.328Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}