In Drupal 7, a database table created through hook_schema() gets created on module installation, and gets deleted on uninstallation.

I would like to prevent the removal of a database table when the module defining it is uninstalled, because the data which I am storing is very important so I want to make sure that even if someone even uninstalls the module for certain purpose than also data retains. Backing up the table before is certainly one solution but I am looking for some better alternative.


Uninstall is there just to delete changes made to database. If you do not want that deletion to happen, do not uninstall. If you need only one table, back it up before uninstallation and restore it after. If you want to remove module from uninstall list, simply delete it's files.

If you need selected module to be always on, you can use hook_modules_disabled() in a helper module, to display message like "Disabling of this module is not permitted" and instantly re-enable it. If module is never disabled, there will be no option to uninstall it. Of course if someone with admin privileges will want to destroy your data, he will, so don't give those privileges to those who can't be trusted with them, but at least it should prevent accidental module removal by developer who is not bright enough to stop and think.

Last but not least, if module's data is of significant importance, you have daily or maybe even hourly backups anyway, so accidental deletion is not a problem at all. Backup and Migrate and Elysia Cron are great help with setting up backups like this, but you should also ask your sysadmin to provide automatic copy to another physical machine, if possible.


I would implement hook_install and hook_uninstall and not use hook_schema.

In hook_install you can use the database api to create the table. Then leave your hook_uninstall blank, thus preserving the table and data.

  • 1
    You cannot: Drupal uses hook_schema() in different functions. Without that, the database table doesn't exist, for Drupal. – kiamlaluno Sep 28 '13 at 19:32

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.