# profile-builder/4.0.0/form-builder/blocks/src/lib/removeBlocksWithUndo.js

User Profile Builder – Beautiful User Registration Forms, User Profiles &amp; User Role Editor, version 4.0.0. 58 lines.

- Page: https://pluginprobe.com/plugins/profile-builder/4.0.0/code/form-builder/blocks/src/lib/removeBlocksWithUndo.js
- Raw: https://pluginprobe.com/plugins/profile-builder/4.0.0/raw/form-builder/blocks/src/lib/removeBlocksWithUndo.js
- Modified: 2026-08-17T08:51:38+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/profile-builder/4.0.0/code/form-builder/blocks/src/lib/removeBlocksWithUndo.js#L10-L20`.

```javascript
/**
 * Remove field blocks with a snackbar Undo (global undo stays off; attribute
 * edits are mirrored server-side). Re-insert on Undo in ascending index order.
 *
 * @param {string[]|string} clientIds
 * @param {Object}          [options]
 * @param {string}          [options.message]
 * @param {Function}        [options.select]
 * @param {Function}        [options.dispatch]
 */

import { select as defaultSelect, dispatch as defaultDispatch } from '@wordpress/data';
import { __ } from '@wordpress/i18n';

export function removeBlocksWithUndo( clientIds, options = {} ) {
    const select   = options.select || defaultSelect;
    const dispatch = options.dispatch || defaultDispatch;

    const ids = ( Array.isArray( clientIds ) ? clientIds : [ clientIds ] ).filter( Boolean );
    if ( ! ids.length ) return;

    const editor = select( 'core/block-editor' );
    const { removeBlocks, insertBlock } = dispatch( 'core/block-editor' );
    if ( ! editor ) return;

    const snapshots = ids
        .map( ( clientId ) => ( {
            block:        editor.getBlock( clientId ),
            rootClientId: editor.getBlockRootClientId( clientId ) || '',
            index:        editor.getBlockIndex( clientId ),
        } ) )
        .filter( ( snapshot ) => !! snapshot.block )
        .sort( ( a, b ) => a.index - b.index );

    removeBlocks( ids );

    const notices = dispatch( 'core/notices' );
    if ( ! notices || ! snapshots.length ) {
        return;
    }

    const message = options.message || __( 'Field removed from this form.', 'profile-builder' );

    notices.createInfoNotice( message, {
        type:    'snackbar',
        actions: [
            {
                label:   __( 'Undo', 'profile-builder' ),
                onClick: () => {
                    snapshots.forEach( ( { block, index, rootClientId } ) => {
                        insertBlock( block, index, rootClientId || undefined );
                    } );
                },
            },
        ],
    } );
}

```
