16

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.

5

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.

6

There is no solution.

The problem is, uninstalling a module means to delete it, but when it is deleted, there is nothing more that could uninstall stuff.

  1. Some extension provider, provide a SQL-Query which clears the database.

  2. One group on one of our hackathons once wrote a module which makes magento core function of using uninstall scripts useable: https://github.com/magento-hackathon/MageTrashApp

Unfortunately you need another module, which does this for you, so you might go with 1.

1

I suppose it would be possible to create a self-destruct file. I would envision something like this:

User clicks "Uninstall Extension and Delete All Data" from the extensions configuration page (of course prompt to confirm after first button click, THIS IS PERMANENT).

Now for the functionality, if you could delete all the module's files first, then delete itself with unlink(__FILE__).

Another idea would be a bit hackish, but say you dynamically created an sql install script, that was actually an uninstall script. Your action on the backend would update your module's version, inject the sql file with the uninstall script, which would then be run on the next Mage load.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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