When my module is installed, I need to execute a batch process, loading hundreds of entities and doing stuff with them.

This is trivial with hook_post_update_NAME() and its $sandbox variable. How can I achieve the same sort of thing via hook_install()?

1 Answer 1


If it's a custom module you can use hook_deploy_NAME(&$sandbox) inside MY_MODULE.deploy.php. drush deploy:hook being executed as part of drush deploy will pick up any hook_deploy_NAME implementation even of just recently enabled modules.

So make sure you execute drush deploy as part of your deployment routine, then your custom module will get enabled first from drush cim (and an entry in the core.extension.yml of course) and your custom install logic will get run from drush deploy:hook being executed after it.

Found out just recently that hook_deploy_NAME implementations won't get registered on module enablement like hook_update_N implementations are. Every existing hook_deploy_NAME implementation will just be picked up.

Talked about it quickly with Moshe Weitzman (the Drush maintainer) on Drupal Slack asking if this is a bug and he said no it's a feature and that hook_deploy_NAME implementations are supposed to only be used in custom modules.

See https://drupal.stackexchange.com/a/254411/15055 for all the commands that are part of drush deploy.

Have you tried batch_set in the install hook? https://drupal.stackexchange.com/a/310282/15055

  • Oh, I totally forgot about hook_deploy_NAME(). Unfortunately, this site is not using drush deploy yet. And for some reason I didn't think you could use batch_set in hook_install(), but you totally can.
    – sonfd
    Mar 29, 2022 at 14:31
  • 1
    I resolved with batch_set
    – sonfd
    Mar 29, 2022 at 14:45
  • Can you post a full example of your use case? Jun 22, 2022 at 19:50
  • 1
    @HodbaKhalaf – You mean the full example that's linked in the last line of my post? drupal.stackexchange.com/a/310282/15055
    – leymannx
    Jun 22, 2022 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.