3

I can't figure how it is possible that the table I define in a hook_schema is created when I enable my module, but it isn't deleted when I uninstall the module (after disabling).

Drupal 7 documentation sais that the table should be automatically created and deleted when these events occours.

Here is the relevant part of the code inside my_module.install:

function my_module_schema(){
    $schema = array();
    $schema["my_module_table_xyz"] = array(
            ...
    );
    return $schema;
}

I don't know if it could be related to the difference between the modue name and the table name... or from the code present in the hook_uninstall... or something else...

Considering I also define a node type, the hook_uninstall should delete it (see following code), then system should automatically uninstall previously defined schema.

function my_module_uninstall(){
    ...
    node_delete_multiple(...);
    // delete custom fields
    node_type_delete(...);
    field_purge_batch(...);
}

What do you think about? Thanks

NOTE: I use Drush to disable, uninstall and reinstall the module and I get the error Table my_module_table_xyz already exist.


Update: I just verified that the same code in a clean Drupal installation works as expected (table defined in hook_schema have been successfully removed).

In the "problematic" Drupal installation I also tried to explicit run drupal_uninstall_schema("my_module_table_xyz") at the end of the hook_uninstall (and I inserted a log instruction just after that line, to verify it runs without breaking), but nothing change. The table is stille there!

Any idea/hypothesis about will be really appreciated.

3
  • 1
    Disabling a module does not explicitly delete nodes created under that custom type - so yes, you would need to do this yourself. Though, most do not do this and instead leave it to the site administrators to make that call.
    – Kevin
    Commented Sep 14, 2016 at 16:31
  • Also if you get a table installed, and then disable a module without the table going away, you will have issues re-enabling the module. In that case, delete the table manually, clear the cache, then enable the module. The table should appear without error. Disable the module, the table should be gone.
    – Kevin
    Commented Sep 14, 2016 at 17:32
  • 1
    @Kevin thanks for your reply. I suppose that every time you wrote "disable" you meant "uninstall". I know that uninstalling a module doesn't delete provided nodes: this question is related to "hook_schema", so I'm not speaking of nodes (the code in my hook_uninstall have just been reported to exlude problem with it). Yes, I'm also aware that if enabling a module it says "unable to create an existing table" I have to manually delete that table, then I can proceed. My question here is: "what is the problem with my code?" and not "what trick I can opt for?" Commented Sep 15, 2016 at 8:48

2 Answers 2

4

I had the same issue and finally I just manually did it in hook_uninstall(), I could find no other recourse.

function mymodule_uninstall() {
  $table_name = 'mytable';
  if (db_table_exists($table_name)) {
    db_drop_table($table_name);
  }
}

Hope this saves someone else some time.

0
1

For recent usage in Drupal 8-9, this is the equivalent:

function mymodule_uninstall() {

  $table_name = 'mytable';
  $schema = \Drupal\Core\Database\Database::getConnection()
    ->schema();
  if ($schema->tableExists($table_name) ) {
    $schema->dropTable($table_name);
  }

}
2

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.