Easily Reinstall Custom Module SQL in Magento

When you are first testing your new custom module, it’s extremely likely your schema change at some point. The method of updating SQL in Magento is to create an upgrade, which involves creating a new upgrade file and incrementing your module version number. If you are just developing and not pushing an official release, there is a shortcut you can take.

Removing Your Custom Module

This solution completely removes the reference to your custom module in the Magento resources table. Essentially, Magento will think this module has been deleted and will try to reinstall it upon refresh. Note that simply disabling your module in the Advanced menu only disables output.

1
DELETE FROM core_resource WHERE code = 'mymodule_setup';
DELETE FROM core_resource WHERE code = 'mymodule_setup';

Having a terminal open (or PhpMyAdmin) and using this SQL repeatedly is also a hassle. Better, but still a hassle. We can reset the module resource programmatically to make it easier.

Programmatically Remove a Custom Module Resource

Remove your module resource from the frontend with the following:

1
2
3
4
5
6
7
8
9
10
11
12
public function removeMyModuleAction() {
    $sql = "DELETE FROM 'core_resource'
                  WHERE 'code' = 'mymodule_setup';";
 
    $connection = Mage::getSingleton('core/resource')->getConnection('core_write');
 
    try {
        $connection->query($sql);
    } catch (Exception $e) {
        echo $e->getMessage();
    }
}
public function removeMyModuleAction() {
    $sql = "DELETE FROM 'core_resource'
                  WHERE 'code' = 'mymodule_setup';";

    $connection = Mage::getSingleton('core/resource')->getConnection('core_write');

    try {
        $connection->query($sql);
    } catch (Exception $e) {
        echo $e->getMessage();
    }
}

Of course, you should remove this reference when you go live. We wouldn’t want a crawler to reset our module every time it visits the page.

You could take it a step further and have the code run on every page request. Overkill for most, but an option if you deem it necessary.

Discussion
freeman says:

It’s good, but what about the fields they related with this module?

Omer says:

Thanx just what I needed, saved me time!

sharpsh007er says:

@freeman, just use
delete from core_config_data where path like ‘module_name/%’

Leave a Reply