6

Suppose you have a custom module, and you have hook_update_N() implementations in your .install file.

If you have old update functions, and all updates have run in all sites that the module is installed, is it safe to delete those functions? Please note this is about custom modules that you have access to all sites and updates have been run.

Thank you in advance and I wish everyone a merry Christmas!

5

As long as you can be sure that they will never be needed it is safe to delete them.

Definitely make sure that you have made any changes required in the main install sections before removing the hook_update_N section.

If possible use some form of version control so you have clear record of the changes if you find you have missed upgrading that module on a site it will be super easy to restore that section for a one-off update.

6

Actually, there is a situation where you could need to keep the older update hooks: when you restored the database from an old backup and you need to update the database to the latest version of the installed modules.
If that is never your case, then—as long as you are sure all the sites have been updated to the same version of the module—you can remove the old update hooks. Keep in mind that, if you delete an update hook you needed for a site, the alternative is uninstalling the module, and then install the latest version, but this will cause data loss.

Updates hooks are only invoked when updating an installed module, and they are not invoked contemporarly to hook_schema() and hook_install(). When a module is installed, hook_update_N() is not invoked; hook_schema() and hook_install() are only invoked when a module is installed. That is the reason why old hook_update_X() could be removed, once a module is updated to version X or older.

Usually, the update hooks are removed when passing from a Drupal version of the module a more recent one (e.g. from the Drupal 6 module to the Drupal 7 module). Since the installation file is loaded just on updating and installing (and other limited situations), removing the update hooks doesn't give any significant gain.

3

Thanks for the other answers, but I'm answering the questions with some of of my findings.

When you define the database schema in a hook_schema, that should be always the latest version of the database. For other hooks, such as hook_install and hook_enable, it's same. All 3 hooks should use the latest functionality as you are installing the module afresh.

Schema version is stored in the system table, along with the module name. To answer my own question, yes I can remove old hook_update_N() functions if I'm sure all sites already have the update.

You can skip update numbers, and Drupal still can figure out the upgrade steps. If your scheme version is 7004, and the module's .install file contains only hook_update_7009, only the 7009 will be called. See update_get_update_function_list. Also, you can implement hook_update_last_removed to inform Drupal about updates that are no longer available, and it will look for greater update Ns.

1
  • Indeed, hook_schema() needs to use the latest schema of the database, since it is called instead of hook_update_N() when the module is being installed, and not updated. Drupal doesn't care for the update numbers; it just runs the update functions starting from the ones with the lower number.
    – apaderno
    Dec 25 '14 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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