codex-team/editor.js · error · Error

Can't find a Block to remove

Error message

Can't find a Block to remove

What it means

BlockManager.removeBlock looks up the block's index in its internal array; if the block is not present (already removed, or not managed by this editor) validateIndex fails and it throws. Callers include delete, backspace, mergeBlocks, removeSelectedBlocks, and clear.

Source

Thrown at src/components/modules/blockManager.ts:533

    this.removeBlock(blockToMerge);
    this.currentBlockIndex = this._blocks.indexOf(targetBlock);
  }

  /**
   * Remove passed Block
   *
   * @param block - Block to remove
   * @param addLastBlock - if true, adds new default block at the end. @todo remove this logic and use event-bus instead
   */
  public removeBlock(block: Block, addLastBlock = true): Promise<void> {
    return new Promise((resolve) => {
      const index = this._blocks.indexOf(block);

      /**
       * If index is not passed and there is no block selected, show a warning
       */
      if (!this.validateIndex(index)) {
        throw new Error('Can\'t find a Block to remove');
      }

      this._blocks.remove(index);
      block.destroy();

      /**
       * Force call of didMutated event on Block removal
       */
      this.blockDidMutated(BlockRemovedMutationType, block, {
        index,
      });

      if (this.currentBlockIndex >= index) {
        this.currentBlockIndex--;
      }

      /**
       * If first Block was removed, insert new Initial Block and set focus on it`s first input

View on GitHub (pinned to 5f45dabbe5)

Solutions

  1. Check the block still exists (getBlockById/getBlockByIndex) before removing
  2. Debounce/flag click handlers so remove is attempted once
  3. Re-acquire block references after render/clear operations

Example fix

// before
blockManager.removeBlock(block);
// after
if (blockManager.blocks.indexOf(block) !== -1) {
  blockManager.removeBlock(block);
}
Defensive patterns

Strategy: try-catch

Validate before calling

if (blockManager.getBlockById(block.id) !== undefined) { blockManager.removeBlock(block); }

Type guard

const isManagedBlock = (bm: BlockManager, block: Block): boolean => bm.blocks.indexOf(block) !== -1;

Try / catch

try { blockManager.removeBlock(block); } catch (e) { if (e instanceof Error && e.message.includes("Can't find a Block")) return; /* already removed */ throw e; }

Prevention

When it happens

Trigger: Removing a Block instance that was already destroyed (double-remove); removing a block belonging to another editor instance; removing during clear() when the list is already empty.

Common situations: Double-click handlers firing twice; undo/redo racing a delete; calling api.blocks.delete on stale BlockAPI references after re-render.

Understand the failure class

Background: "Not found" and "does not exist" errors: why "Task not found", "No such folder", and "Can't find" fire when a lookup comes back empty — this error's family across 14 libraries.

Related errors


AI-assisted analysis of codex-team/editor.js@5f45dabbe5 (2026-08-27). Data as JSON: /api/errors/8a298a2927387aca. Report an issue: GitHub.