Disable hooks during migration

Last updated on
30 April 2025

Quite frequently, hooks defined by other modules firing during migration (as a result of node_save(), user_save(), etc.) cause various problems - most often it's simply a performance impact (use the timer instrument to identify these), but sometimes it's actually dangerous (such as sending email notifications). With Migrate 2.6 and later, hooks belonging to other modules can be disabled in the migration configuration.

In your hook_migrate_api() implementation, add an array keyed by 'disable_hooks' to the arguments to your group or migration definition. Each entry in the array will be keyed by a hook name, and the value will be an array of modules for which to disable that hook. Here's a real-world example for a migration into Drupal Commons, where there are many, many hooks firing and substantial pruning is needed to achieve reasonable migration performance:

  $disable_hooks = array(
    'comment_insert' => array(
      'commons_activity_streams',
      'commons_follow_node',
      'commons_notify',
      'commons_radioactivity',
      'search',
      'expire',
    ),
    'comment_publish' => array(
      'search',
      'xmlsitemap_node',
      'expire',
    ),
    'comment_update' => array(
      'commons_activity_streams',
      'commons_follow_node',
      'commons_notify',
      'commons_radioactivity',
      'search',
    ),
    'entity_insert' => array(
      'metatag',
      'og',
      'registration',
    ),
    'entity_presave' => array(
      'pathauto',
    ),
    'entity_update' => array(
      'metatag',
      'og',
      'registration',
    ),
    'flag' => array(
      'commons_radioactivity',
    ),
    'node_insert' => array(
      'commons_activity_streams',
      'commons_follow_group',
      'commons_follow_node',
      'commons_groups',
      'commons_notify',
      'commons_radioactivity_groups',
      'kwresearch',
      'pathauto',
      'xmlsitemap_node',
      'expire',
    ),
    'node_update' => array(
      'commons_activity_streams',
      'commons_follow_group',
      'commons_follow_node',
      'commons_groups',
      'commons_radioactivity_groups',
      'kwresearch',
      'pathauto',
      'xmlsitemap_node',
      'expire',
    ),
    'node_prepare' => array(
      'og_menu',
    ),
    'taxonomy_term_insert' => array(
      'metatag',
      'pathauto',
      'shs',
      'taxonomy_menu',
    ),
    'taxonomy_term_update' => array(
      'pathauto',
      'shs',
      'taxonomy_menu',
    ),
    'user_insert' => array(
      'pathauto',
    ),
    'user_update' => array(
      'pathauto',
    ),
    'votingapi_insert' => array(
      'expire',
    )
  );

  $api = array(
    'api' => 2,
    'groups' => array(
      'example' => array(
        'title' => 'Example Project',
        'disable_hooks' => $disable_hooks,
      ),
    ),
    'migrations' => array(
...

Help improve this page

Page status: Not set

You can: