Any module installed using composer can be uninstalled using bin/magento module:uninstall [Vendor]_[ModuleName]

During the uninstall process Magento looks for an uninstall class located in the module being uninstalled. Specifically for Setup/Uninstall.php. It does this in the \Magento\Setup\Model\UninstallCollector.php class (collectUninstall()).

If it finds an Uninstall file and that file is a subclass of Magento\Framework\Setup\UninstallInterface it will run it very similar to how it runs install files. What does an Uninstall script look like?

<?php

namespace [Vendor]\[ModuleName]\Setup;

use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\UninstallInterface;

class Uninstall implements UninstallInterface
{
    public function uninstall(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        // @todo put uninstall logic in here
    }
}

What can i use this for?

Well if your module installed some database tables you could use this to remove those tables from the database as if the module was never installed in the first place.

For example:

$setup->getConnection()
    ->dropTable('my_custom_table');

This is in stark contrast to Magento 1.x whereby uninstalling a module left traces of itself that you had to clear up manually.

Note At time of writing there seems to be a bug in Magento whereby the uninstall process never completes leaving the site in maintenance mode. See Github issue that i raised earlier.

  • magento2

Like this post? Share it :)


Related Posts

Back