Undo and redo
Revert and restore your changes, using the undo and redo features.
Overview
The UndoRedo plugin records the actions you take in the grid and stores them in undo and redo stacks.
You can use keyboard shortcuts, the context menu, or API methods to move backward and forward through that history.
The plugin is enabled by default. When the context menu is enabled, its default items include Undo and Redo.
Basic demo
Make a few edits in the grid.
Press Ctrl/⌘+Z to undo your last action.
Press Ctrl/⌘+Y (or Ctrl/⌘+Shift+Z) to redo it.
import Handsontable from 'handsontable/base';import { registerAllModules } from 'handsontable/registry';// Register all Handsontable's modules.registerAllModules();const container = document.querySelector('#example');new Handsontable(container, { data: [ ['Ana García', 'Engineering', 'Senior Engineer', 95000, '2026-03-14'], ['James Okafor', 'Marketing', 'Product Manager', 88000, '2026-07-01'], ['Li Wei', 'Engineering', 'Frontend Dev', 82000, '2026-01-10'], ['Maria Santos', 'HR', 'HR Specialist', 71000, '2026-11-20'], ['David Kim', 'Engineering', 'Backend Dev', 85000, '2026-08-05'], ['Emma Wilson', 'Marketing', 'SEO Analyst', 68000, '2026-02-14'], ['Ahmed Hassan', 'Finance', 'Controller', 92000, '2026-06-30'], ['Sara Johansson', 'Engineering', 'QA Engineer', 78000, '2026-09-12'], ['Carlos Mendez', 'Sales', 'Account Manager', 74000, '2026-04-25'], ], rowHeaders: true, colHeaders: true, stretchH: 'all', height: 'auto', autoWrapRow: true, autoWrapCol: true, // licenseKey: 'your-license-key', // get a key at https://handsontable.com/pricing/});import Handsontable from 'handsontable/base';import { registerAllModules } from 'handsontable/registry';
// Register all Handsontable's modules.registerAllModules();
const container = document.querySelector('#example')!;
new Handsontable(container, { data: [ ['Ana García', 'Engineering', 'Senior Engineer', 95000, '2026-03-14'], ['James Okafor', 'Marketing', 'Product Manager', 88000, '2026-07-01'], ['Li Wei', 'Engineering', 'Frontend Dev', 82000, '2026-01-10'], ['Maria Santos', 'HR', 'HR Specialist', 71000, '2026-11-20'], ['David Kim', 'Engineering', 'Backend Dev', 85000, '2026-08-05'], ['Emma Wilson', 'Marketing', 'SEO Analyst', 68000, '2026-02-14'], ['Ahmed Hassan', 'Finance', 'Controller', 92000, '2026-06-30'], ['Sara Johansson', 'Engineering', 'QA Engineer', 78000, '2026-09-12'], ['Carlos Mendez', 'Sales', 'Account Manager', 74000, '2026-04-25'], ], rowHeaders: true, colHeaders: true, stretchH: 'all', height: 'auto', autoWrapRow: true, autoWrapCol: true, // licenseKey: 'your-license-key', // get a key at https://handsontable.com/pricing/});What UndoRedo tracks
UndoRedo records every action you take in the grid, whether you use the UI or the API. Each action is one undo step.
Recording starts when the grid is created, so the calls your app makes to set the grid up are steps too. To start with an empty history, call clear() after your setup code. With undo: false, nothing is recorded, and the plugin’s undo() and redo() do nothing.
The tracked actions include:
- Cell value changes: editing, pasting, autofill, clearing cells, and API calls such as
setDataAtCell(),populateFromArray(), andsetSourceDataAtCell(). - Inserting and removing rows and columns.
- Sorting and filtering.
- Moving rows, moving columns, and moving cells, when the
moveCellsoption is enabled. - Merging and unmerging cells, and changing cell alignment.
- Cell metadata written with
setCellMeta()orremoveCellMeta(), for example aclassNameor areadOnlyflag. - Making cells read-only or writable through the Read only item of the context menu or the column menu. The whole selection is one step.
- Hiding and showing rows and columns, and trimming rows.
- Resizing rows and columns, through the API or by dragging a header. A whole drag is one step.
- Freezing and unfreezing columns.
- Collapsing and expanding nested rows, adding a child row, and detaching a row from its parent.
- Collapsing and expanding collapsible column headers.
- Custom borders.
- Comments: adding, editing, and removing a comment, and making it read-only.
- The pagination page and page size.
Make an API change undoable
You don’t need to register changes you make with the API. UndoRedo records them the same way it records edits in the UI.
hot.setDataAtCell(0, 0, 'New value');
// The user can press Ctrl/Cmd+Z to revert the change,// or you can revert it from code:hot.getPlugin('undoRedo').undo();To make several API calls revert with one undo, group them with batch(). See One action, one undo step.
For a change that UndoRedo doesn’t record, see Known limitations. To see what clears the history, for example loadData(), see Hooks and stack lifecycle. To cover state that lives outside the grid, register your own step with done().
One action, one undo step
UndoRedo records everything a single action changes as one step. When a paste adds rows to satisfy minSpareRows, one undo removes the pasted values and the added rows together.
Calls you group with batch() or batchExecution() are one step too. The step’s actionType is 'batch'.
// One undo reverts the edit, the removal, and the sorthot.batch(() => { hot.setDataAtCell(0, 0, 'New value'); hot.alter('remove_row', 5); hot.getPlugin('columnSorting').sort({ column: 0, sortOrder: 'asc' });});To group your own changes without suspending rendering, use runOperation(). The operation name becomes the step’s actionType, and the source is passed to the stack hooks.
hot.runOperation('import', () => { hot.setDataAtCell(0, 0, 'A'); hot.setDataAtCell(1, 0, 'B'); hot.alter('remove_row', 5);}, 'myImport');Operations nest. A batch() or runOperation() call inside another one joins the outer step.
An edit that waits for an asynchronous validator is recorded when the validation finishes. So when two edits overlap, the undo stack holds them in the order they finished. When a validator rejects every change of an edit, nothing is recorded. What the validator or an afterValidate listener changes while the edit is validated, such as a corrected value, is part of the edit’s step, so one undo reverts both.
An action that changed the grid before it started to wait, such as a batch() that removes a row and then edits a validated cell, owns the grid until its validation finishes. Anything you change in that time becomes part of its step, so one undo reverts both. Undone apart, one of the two would land on the wrong row. This applies only to an action that UndoRedo records: an action with the auto source records no step, so what you change while it waits is a step of its own. A validator that throws an error ends the wait: the step is recorded with what the action did before the error.
While such an action waits for its validator, isUndoAvailable() and isRedoAvailable() return false, and undo() and redo() do nothing. The grid then holds half of a step that no recorded state describes. An undo requested in that time is not queued. An edit on its own changes nothing before its validation finishes, so it does not block them.
How an undo restores the grid
For each step, UndoRedo stores the grid state before and after the action, and a record of the values and the cell metadata the action changed. The grid state covers the row and column order, the hidden and trimmed rows and columns, the sizes, the sort order, the filter conditions, the merged cells, and the state of the other plugins. An undo puts back the state from before the action, and a redo puts back the state from after it.
An undo puts each value back on the record you edited, even when that row was moved, sorted, filtered, or trimmed since. Handsontable DataGrid writes the value straight to the source data. Four things follow from that:
beforeChangedoes not run for restored values, and neither does a cell’svalueSetter. The restored value is the value the source data held.afterChangeruns once per undo or redo, with every visible cell the restore wrote and theUndoRedo.undoorUndoRedo.redosource.afterSetSourceDataAtCellruns too.- Handsontable validates the restored visible cells again. The value stays as restored, and only the cell’s
validstate is updated. - A restored filter or sort is not recalculated. The grid shows the rows exactly as they were.
An undo or a redo does not run the action again, so the action’s own hooks — such as beforeColumnSort, beforeFilter, beforeRowMove, or beforeMoveCells — do not fire and cannot cancel it. To cancel an undo or a redo, return false from beforeUndo or beforeRedo.
Rows and columns that an undo or a redo adds or removes do go through the row and column hooks, such as beforeCreateRow and beforeRemoveRow. When one of those hooks returns false, Handsontable puts the grid back as it was, the step stays on its stack, and afterUndo or afterRedo does not fire.
Hooks and stack lifecycle
UndoRedo exposes hooks for both stack updates and action execution:
- Stack update hooks:
beforeUndoStackChange,afterUndoStackChange,beforeRedoStackChange, andafterRedoStackChange. - Action hooks:
beforeUndo,afterUndo,beforeRedo, andafterRedo.
You can return false from beforeUndoStackChange, beforeUndo, or beforeRedo to block recording or execution.
The action hooks receive the step. beforeUndo and beforeRedo fire before the stack hooks, so in beforeUndo the step is still on the undo stack. The hooks receive the step object the stack holds, not a copy, so treat it as read-only. Every step the grid records has these properties:
actionType: the name of the action, for example'change','remove_row','col_move','hide_rows', or'batch'.source: the source of the action.operationsandsources: the name and the source of every operation the step contains, in order.
Some action types add their own properties. A 'change' step has changes (an array of [row, column, oldValue, newValue], with visual indexes, in the order the cells were passed) and selected. A 'remove_row' step has index, amount, indexes, and data. A cellRange, range, or ranges property holds plain objects with the range’s coordinates, for example step.cellRange.from.row, not CellRange instances.
Both stacks are cleared when:
- You call
loadData(), orupdateData()with other rows. The recorded steps describe the dataset those methods replace.updateData()keeps the history when the new data is what the grid shows already: the same array, a copy of it, or rows with the same values, as a wrapper re-render gives. Cells are compared with===, so a nested object that you copied counts as a different value, and withNestedRowsa copy of the array clears the history too. Rows you reordered or replaced in the array in place clear it as well. - You turn on or turn off, at runtime, a plugin that keeps its own index map — for example with
updateSettings({ hiddenRows: true }). That covershiddenRows,hiddenColumns,trimRows, the sorting plugins, andfilters, but not the move plugins. Passing a plugin’s settings again, as a framework wrapper does on every render, keeps the stacks. - The number of rows changes outside any recorded action, or the number of columns changes through a setting other than
columns. Empty rows and columns that Handsontable adds at the end on its own, for example forminSpareRows, keep the stacks. - An action you recorded with
done()adds or removes rows or columns anywhere but at the end when you undo or redo it. - You disable the plugin or destroy the grid.
A columns update, for example one that shows fewer fields, keeps the stacks. An edit is recorded by field, so a columns update never drops it on its own. A step that changed something on a column whose field changed or is gone — a cell’s metadata, a hidden or frozen column, a merge, or a filter — is dropped, together with every step that can only be undone or redone after it, edits included. Once columns is set, a step that inserted or removed columns is dropped too.
The stack hooks announce every drop, so a toolbar that follows them stays in step.
Limit the history
By default, the undo stack has no size limit. To keep only the most recent steps, set undo to an object with a maxHistory number. Once the stack grows past the limit, UndoRedo drops the oldest step.
maxHistory takes a whole number of 0 or more, or Infinity. With 0, UndoRedo keeps no steps. A lower limit set with updateSettings() drops the oldest steps at once. Any other value logs a warning and keeps the previous limit.
const hot = new Handsontable(container, { undo: { maxHistory: 100, },});Programmatic control
Use the plugin instance to inspect and control history:
const undoRedo = hot.getPlugin('undoRedo');
if (undoRedo.isUndoAvailable()) { undoRedo.undo();}
if (undoRedo.isRedoAvailable()) { undoRedo.redo();}
undoRedo.clear();Registering a custom undoable action
Everything you change in the grid is recorded for you. Use done() for state that lives outside the grid, so that one undo stack covers both.
Call done() with a function that returns an action object. The action object needs an undo() method and a redo() method, each receiving the Handsontable instance and a callback to call once the operation finishes.
const favoriteRows = new Set();
function toggleFavoriteRow(row) { const undoRedo = hot.getPlugin('undoRedo'); const toggle = () => { if (favoriteRows.has(row)) { favoriteRows.delete(row); } else { favoriteRows.add(row); }
hot.render(); };
undoRedo.done(() => ({ actionType: 'favoriteRow', undo(instance, callback) { toggle(); callback(); }, redo(instance, callback) { toggle(); callback(); }, }), 'favoriteRow');
toggle();}After you call toggleFavoriteRow(), pressing Ctrl/⌘+Z reverts the change, and pressing Ctrl/⌘+Y reapplies it.
Do not register a change to the grid with done(). UndoRedo already records it, so the change would take two undo steps.
Undo and redo with formulas
With the Formulas plugin, the grid’s undo stack covers the formula engine too. An undo restores the formulas and the values in the grid, and Handsontable updates the engine to match.
The engine’s own undo stack is not used. If you call HyperFormula’s undo() or redo() yourself on an engine that a grid is connected to, the grid and the engine get out of sync. Use the grid’s undo instead.
When several grids share one engine, adding, removing, or moving rows, columns, or cells in one grid can change the formulas of the other grids and of named expressions that refer to it. An undo puts those formulas back, and a redo applies the change again. The other grids get no undo step of their own. Two limits apply:
- A formula edited in another grid after the step keeps its new text, and the grid logs a warning.
- A formula written in another grid after the step is not adjusted by the undo.
Known limitations
UndoRedo does not record every change.
The following changes are not recorded:
- The size of a comment box that you resize by dragging.
- A page change in a grid whose pages come from a server through the
dataProvideroption. - Merges applied from the
mergeCellssetting. Merges and unmerges done through the context menu, the keyboard shortcut, and the plugin’smerge()andunmerge()methods are recorded. - Rows and columns that Handsontable adds on its own to satisfy
minRows,minCols,minSpareRows, orminSpareCols, outside any action. They carry theautosource, which UndoRedo skips. Rows added this way during an edit are part of that edit’s step. - Sizes that Handsontable calculates, such as the ones from
autoRowSize,autoColumnSize, andstretchH. - Changes you make to the data array directly, without the Handsontable API.
More behaviors to plan for:
- An undo of an action that inserted or removed rows or columns puts back the row and column order and the sizes as they were when the action ran. An order or a size applied with
updateSettings()after that action is lost on the undo. Hidden and trimmed rows and columns are kept. - In a nested rows grid, a
batch()that both moves or detaches rows and edits cells can put an edit on another row when you undo it. - A column whose
datais a function your app creates again on every render, such as an inline function in a React component, reads as a new field on everycolumnsupdate. So each render drops the steps that changed something on that column. Define the function once, outside the render. - When a removal that an undo or a redo replays removes a different number of rows or columns than it recorded, for example because a listener removes more rows along with them, the undo or redo stops and the rows that removal took are not put back.
Related keyboard shortcuts
| Windows | macOS | Action | Excel | Sheets |
|---|---|---|---|---|
| Ctrl+Z | ⌘+Z | Undo the last action | ✓ | ✓ |
| Ctrl+Y | ⌘+Y | Redo the last action | ✓ | ✓ |
| Ctrl+Shift+Z | ⌘+⇧+Z | Redo the last action | ✓ | ✓ |
Related blog articles
Related API reference
Configuration options
Core methods
Hooks
Plugins
Microsoft and Excel are registered trademarks of Microsoft Corporation. Google Sheets is a trademark of Google LLC.