Revert a Drupal Database Update

Mark's picture

Site Section: 


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.


Nice trick!

Nice trick! You may want to add that this will only work if you are not doing an irreversible change to the DB. For e.g. if you are creating a field named "field_xyz" and you run hook_update_N() again, it will fail since the field and corresponding table already exists.


I was coming here anticipating to read about restoring backups, maybe cleverly knowing what changes were made and maybe partially restoring a backup, or something. Basically, some sort of (attempted) solution to what is the achilles heal of Drupal updates: rollbacks.

Don't get me wrong, the trick is useful, but what you're doing is rerunning an update hook. Maybe change the title of your post to manage expectations :)

Also, you may want to make it more clear that the XXXX in the query needs to be the the N of the update hook *prior* to the one you want to rerun (or 1 less than the one you need to rerun, if there are no others).

this is not a revert

This is not a revert of the db update, you're only fooling Drupal into thinking it needs to run (again).

Depending on the nature of the update and what has been done when you first tried to run it, running the update hook again may work, fail with errors, or have unexpected side effects.

I would never recommend this unless you know exactly what you're doing. And even if you know exactly what you're doing, there is no substitute for proper db backups.

Add your comment