Revert a Drupal Database Update

Sometimes, when testing an update hook to a Drupal module, you may need to revert it.

For example, you are setting some permissions for a certain role, you run the update but then realise there was another permission you should have enabled. Instead of adding another update hook, you can revert the update you just ran, amend your update hook, and then re-run the update.

Note*: this is only for reversible changes - like adding/removing permisisons; it will fail if the update had created a new field for example.

It's as simple as this SQL query:
update system set schema_version = XXXX-1 where name = 'module_name';

This means, you are telling your database to update the table 'system' using scheme_version 'XXXX-1' (this will be something like 7001 or 7137 or whatever N equals in your hook_update_N() function minus 1, so if the update was hook_update_7138 you would use 7137 where XXXX-1 is), and apply this update to 'machine name of the module'.

Note*: Do not try this unless you know what you are doing. Make sure you have a backup of your database in case things go wrong - of course you do, don't you?

*Note - notes were added from items left in the comments, to bring them to the body.

Filed Under:

  1. Drupal Planet
  2. Drupal Tutorials
  3. SQL