Magento 1.9 – Uninstallation Module Guide

deleteextensionsmagento-1.9

I have created an extension that creates a category attribute when installed first time. But now when I disable/Uninstall the extension, I get an error message on "Manage Categories" page.

I know that when an Extension is disabled via Magento connect, only files are deleted and nothing is deleted from database.

So, In order to overcome this problem can we provide a button to delete database entries that can be placed under system configuration section along with other setting of extension. And when admin clicks on that button all database entries should be deleted along with files used by extension.

Please let me know if above solution will work? or is there any better solution in order to delete unwanted entries from database while uninstalling extension.

Best Answer

You could create an uninstall shell script that lives in the shell/ folder. That file can remove files, directories, database tables, core_resource entries, and attributes from EAV.

It would look something like this:

<?php

include_once 'abstract.php';

class Namespace_Module_Uninstall extends Mage_Shell_Abstract {

    public function run() {
        $this->removeDirectories();
        $this->removeAttributes();
    }

    /**
     * Remove file system files here.
     */
    public function removeDirectories() {
        $file = new Varien_Io_File();

        $file->rmdir(BP . DS . 'app/code/local/My/', true);
        $file->rm(BP . DS . 'app/etc/modules/My_Module.xml');
    }

    /**
     * Remove any attributes here
     */
    public function removeAttributes() {
        $installer = $this->_getSetup();

        $installer->startSetup();

        // repeat this for any other attributes you wish to uninstall
        $installer->removeAttribute('catalog_product', 'your_attribute');

        $installer->endSetup();
    }

    /**
     * Return catalog/customer/core or whichever resource setup class you need
     *
     * @return Mage_Catalog_Model_Resource_Setup
     */
    protected function _getSetup() {
        return Mage::getResourceSingleton('catalog/setup', 'default_setup');
    }
}

$uninstall = new Namespace_Module_Uninstall();

$uninstall->run();

You can run it on the command line with:

php shell/uninstall.php

Once that's done, you can delete the shell file itself.

Related Topic