linshenkx/prompt-optimizer · error · StorageError

Failed to clear storage

Error message

Failed to clear storage

What it means

StorageError with code 'clear' thrown by DexieStorageProvider.clearAll when db.storage.clear() rejects. Like the other Dexie wrappers, it indicates the connection is closed, blocked by a version-change transaction, or IndexedDB is unavailable/corrupted.

Source

Thrown at packages/core/src/services/storage/dexieStorageProvider.ts:147

    try {
      await this.db.storage.delete(key);
    } catch (error) {
      console.error(`Failed to remove storage item (${key}):`, error);
      throw new StorageError(`Failed to remove item: ${key}`, 'delete');
    }
  }

  /**
   * 清空所有存储
   */
  async clearAll(): Promise<void> {
    await this.initialize();
    
    try {
      await this.db.storage.clear();
    } catch (error) {
      console.error('Failed to clear storage:', error);
      throw new StorageError('Failed to clear storage', 'clear');
    }
  }

  /**
   * 原子更新操作
   * 使用 Dexie 的事务机制确保原子性,带重试和降级机制
   */
  async atomicUpdate<T>(
    key: string,
    updateFn: (currentValue: T | null) => T
  ): Promise<void> {
    await this.initialize();

    // 获取键级别的锁
    const lockKey = `atomic_${key}`;
    if (this.keyLocks.has(lockKey)) {
      await this.keyLocks.get(lockKey);
    }

View on GitHub (pinned to 3e677b1d9f)

Solutions

  1. Check the logged underlying error (console.error 'Failed to clear storage:')
  2. Close other app tabs / ensure no pending version upgrade, then retry clearAll
  3. If the DB is corrupt, delete the database (Dexie.delete(dbName)) and let the app recreate it

Example fix

// before
await provider.clearAll();

// after
try { await provider.clearAll(); }
catch (e) {
  if (e instanceof StorageError && e.code === 'clear') {
    await provider.db.delete().then(() => provider.initialize()); // recreate
  } else throw e;
}
Defensive patterns

Strategy: fallback

Validate before calling

if (!provider.db.isOpen()) await provider.db.open();

Type guard

const isStorageClearError = (e: unknown): e is StorageError =>
  e instanceof StorageError && (e as StorageError).code === 'clear';

Try / catch

try { await provider.clearAll(); } catch (e) { if (isStorageClearError(e)) { await provider.db.delete(); await provider.initialize(); } else throw e; }

Prevention

When it happens

Trigger: Calling clearAll during a pending version upgrade in another tab, after db.close(), or in a context where IndexedDB is blocked (private mode, corrupted database file).

Common situations: 'Reset all data' features failing because a stale tab holds the old DB version; running cleanup after the storage layer was disposed; incognito environments.

Related errors


AI-assisted analysis of linshenkx/prompt-optimizer@3e677b1d9f (2026-08-27). Data as JSON: /api/errors/5a17a4cb43687940. Report an issue: GitHub.