0

I have created a custom content-type in my custom module with custom fields.. It works fine.. But when I try to uninstall it, it doesnt call hook_unistall().. Can any one suggest what can be the problem ?

Here is my code :

function homepage_blocks_uninstall() {
    $sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
    $result = db_query($sql, array(':type' => 'homepage_blocks'));

    //echo "hello";
    //exit;

    $nids = array();
    foreach ($result as $row) {
        $nids[] = $row->nid;
    }

    node_delete_multiple($nids);
    foreach (array_keys(_homepage_blocks_installed_fields()) as $field) {
        field_delete_field($field);
        //db_drop_table($field);
    }

    $instances = field_info_instances('node', 'homepage_blocks');
    foreach (_homepage_blocks_installed_instances() as $instance_name => $instance) {
        field_delete_instance($instance);
    }

    node_type_delete('homepage_blocks');
    field_purge_batch(1000);
    //variable_del('comment_homepage_blocks');
    //variable_del('node_submitted_homepage_blocks');
    drupal_flush_all_caches();
    //module_disable('homepage_blocks');
}

And I want to delete all tables, cache, variables node entries related to that module while uninstalling module so that I can re-install it.. What extra code I need ?

_homepage_blocks_installed_fields() and _homepage_blocks_installed_instances() are functions reside in homepage_blocks.functions.inc file and I have included this file to my homepage_blocks.module and homepage_blocks.install

2 Answers 2

3

Are the functions you are calling from your implementation of hook_uninstall() defined in your .module file? If that is the case, those functions will not be found, as the module file is not anymore loaded in memory.

When hook_uninstall() is called, your module will already be disabled, so its .module file will not be automatically included. If you need to call API functions from your .module file in this hook, use drupal_load() to make them available. (Keep this usage to a minimum, though, especially when calling API functions that invoke hooks, or API functions from modules listed as dependencies, since these may not be available or work as expected when the module is disabled.)

4
  • but its not going inside this homepage_blocks_uninstall() function..
    – Amb
    Apr 5, 2013 at 10:06
  • 1
    is your uninstall function defined in homepage_blocks.install file? Are you sure, you are uninstalling your module, not just deactivating it? Apr 5, 2013 at 10:09
  • yes it is inside homepage_blocks.install file And yes I want to completely uninstall this module.
    – Amb
    Apr 5, 2013 at 10:13
  • @amss Are you really uninstalling the module? In Drupal, disabling a module, and uninstalling it are two different things. Did you visit admin/modules/uninstall, select your module name, and clicked on the form button to uninstall it?
    – apaderno
    Apr 5, 2013 at 10:24
0
function homepage_blocks_uninstall() {
    $query = db_select('node','n');
    $query->fields('n', array('nid'))
            ->condition('type', 'homepage_blocks')
    $data=$query->execute();
    while($result=$data->fetchObject()){
        node_delete($result->nid);

    }
    drupal_flush_all_caches();

}

I think it will work or u can implement this by batch process also... :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.