2

I heard that when uninstalling a module that is a content type, you should not use node_type_delete('my_module') in hook_uninstall(). Instead, you should let it be handled by Drupal that just disables the content type in the database. Is this right? Apparently Drupal handles it in hook_node_info().

Wouldn't it be better to delete this node type in the database, or does Drupal handle uninstallation process in an other way?

I am using Drupal 7.

0

2 Answers 2

2

You should not delete content types in hook_disable(). Instead, you should make sure callbacks are left sane, and all data you will need on hook_enable() is properly archived and secured from modification by other modules. Good idea is to use drupal_set_message() to warn about content type that should no longer be used or displayed, but that's site admin's responsibility.

On hook_uninstall() you are supposed to left the database as if your module was never there. Thus, feel free to delete content types that was created using node_type_save(), if there are any. The ones created with hook_node_info() will be deleted automatically. Again, setting message about orphanted content is good idea, but it's site admin's responsibility not to uninstall module he is still using, after all.

If your hook_node_info() defined content type is not deleted, just disabled, you can decide to explicitly delete it, but you are responsible for cleaning all the mess it may cause, like orphaned content. You risk serious site failure if you don't.

Edited to incorporate information from Clive's comments under other answer

3
  • So you say I should not use node_type_delete in hook_uninstall (in file .install) if I have hook_node_info in .module? Because now I use hook_node_info and I create fields and instances in hook_install but I don't use node_type_save. And in hook_uninstall I take care of removing previously created fields and instances. But the thing is when I uninstall the module, the content type remains in node_type table but it is now disabled. Is this the normal behavior? May 22, 2013 at 9:07
  • Yes it is - if you have any content of that content type, or if for some other reason Drupal "thinks" it may be unclean to remove it entirely. Of course you are allowed to force-delete it, but then you are responsible for all the cleaning that is needed.
    – Mołot
    May 22, 2013 at 9:10
  • Ok thank you. I currently remove all nodes of this type in my hook_uninstall but the content type remains in db. May 22, 2013 at 9:12
0

There are two functions which can help you out of this problem with writing hook_uninstall():

  1. node_type_delete is for deleting the content type
  2. node_delete is for deleting the associated nodes
1
  • node_type_delete seems to be in conflict with the way hook_node_info handles the node type. I get an error : May 22, 2013 at 8:53

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.