Hi

TOPIC: Implementing a hook_uninstall()
REFERENCE: Custom module example: node_example_uninstall

PROBLEM?: When calling a hook_uninstall(), it does NOT call implicitly any of the following:

  1. node_delete()
  2. hook_delete()
  3. hook_nodeapi:$op= delete
  4. hook_nodeapi:$op= delete revision

This surprises me!

Since none of the aforementioned calls are made during an uninstall, then this leads to TABLE {node} filling with deprecated (i.e. garbage) entries because they are no longer referred to by custom module's schema.

For example, node_example_schema maintains both vid and nid as a reference to an entry in the TABLE {node}.

When TABLE {node_example} is reinstalled, then the old node entries in the TABLE {node} that were previously reference by the entries of TABLE {node_example} prior to uninstall are all basically garbage. Am I wrong here?

SOLUTION?: Upon hook_uninstall(), should not one recursively call node_delete() on all its node references within their custom module's schema BEFORE calling drupal_uninstall_schema()

Should not node_example_uninstall really be implemented as follows to keep TABLE {node} garbage free:

function node_example_uninstall() {
  $result = db_query( 'SELECT nid FROM {node_example}' );
  while($row = db_fetch_array($query_result)) {
    $nid = $row['nid']
    node_delete($nid);
  }

  drupal_uninstall_schema('node_example');
}

If not, then why not?

Thanks

Jeff in Seattle

Comments

ludo.r’s picture

I dont know if a module should delete its related nodes upon uninstall.

What do you think of that?

Should a module delete its related nodes when uninstalling?

dani.latorre’s picture

When you execute drupal_uninstall_schema it deletes all the tables associated at the module. So all nodes of that module will be deleted too.

What you could check is if there are content types or views using that node type, but it would take so much resources.

ludo.r’s picture

i dont agree. When i uninstall my module, i use drupal_uninstall_schema().

My module's tables are indeed dropped, but the nodes created with my module's content type still remain in the {node} table.

Should they be deleted?

adaddinsane’s picture

@dani - that's not entirely true.

@jeff - what you perceive as a bug is intentional. (Imagine, many years of Drupal and nobody noticed...?)

For a start, it can't be automatic, that would require Drupal to be able to understand what's intended by a schema ( and not all schemas have to do with nodes). And even before there were schemas, if a node_type was removed by a module being uninstalled ... how could the system know without considerable complexity being added.

I seem to recall there are other practical reasons but they escape me for the moment.

If you want to delete a specific node type when your module is uninstalled - then that's up to you.

Steve Turnbull, D8 developer

Like Science Fiction, Fantasy or Steampunk stories? Check out https://taupress.com/book-list

dani.latorre’s picture

Sorry becouse I didn't understand you.

I'm agree with adaddinsane. I just wanna say that you could just unpublish the content by changing the status attribute from {node} table and that you can use the type attribute to know which nodes to change. You don't really need to delete a content.