To uninstall modules in Magento 2, you can either use a Composer command if the module has been installed via Composer, or remove the module manually if it was installed that way.
If you install a module via Composer, you can use Magento’s module:uninstall
command which can be run from the root directory of your Magento project.
Please note that this command only works with Composer packages, otherwise you will encounter the following error.
$ /path/to/your/php bin/magento module:uninstall [Vendor]_[Module]
[Vendor]_[Module] is not an installed composer package
Other modules may depend on the module you are trying to uninstall. If this is the case then you will be prompted and the process will be cancelled.
Cannot uninstall module '[Vendor]_[Module]' because the following module(s) depend on it:
[Vendor]_[ModuleTwo]
If running the command without passing in any arguments, you may get output in the console that looks like the following:
$ /path/to/your/php bin/magento module:uninstall [Vendor]_[Module]
You are about to remove code and/or database tables. Are you sure?[y/N]y
Enabling maintenance mode
You are about to remove a module(s) that might have database data. Remove the database data manually after uninstalling, if desired.
Removing [Vendor]_[Module] from module registry in database
Removing [Vendor]_[Module] from module list in deployment configuration
Removing code from Magento codebase:
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Removing [package] (1.0.0)
Writing lock file
Generating autoload files
Cache cleared successfully.
Generated classes cleared successfully.
Alert: Generated static view files were not cleared. You can clear them using the --clear-static-content option. Failure to clear static view files might cause display issues in the Admin and storefront.
Disabling maintenance mode
During this uninstall process, the composer remove
command is run to remove the code from your project.
This can be seen within Magento’s Magento\Setup\Model\ModuleUninstaller
class, within the uninstallCode()
method.
// setup/src/Magento/Setup/Model/ModuleUninstaller.php
<?php
namespace Magento\Setup\Model;
use Symfony\Component\Console\Output\OutputInterface;
class ModuleUninstaller
{
....
/**
* Run 'composer remove' to remove code
*
* @param OutputInterface $output
* @param array $modules
* @return void
*/
public function uninstallCode(OutputInterface $output, array $modules)
{
$output->writeln('Removing code from Magento codebase: ');
$packages = [];
/** @var \Magento\Framework\Module\PackageInfo $packageInfo */
$packageInfo = $this->objectManager->get('Magento\Framework\Module\PackageInfoFactory')->create();
foreach ($modules as $module) {
$packages[] = $packageInfo->getPackageName($module);
}
$this->remove->remove($packages);
}
}
It is common practice to back up your code and database before running the uninstall command, and this is where you pass in the arguments.
--backup-code
– Backs up the Magento file system (excluding var
and pub/static
directories).--backup-media
– Backs up the pub/media
directory--backup-db
– Backs up the Magento database.All of the backups can be found within the var/backups
directory.
There is also a -r
(or --remove-data
) argument, that when specified, removes the database schema and data defined in the module’s Uninstall classes within the Setup
directory. This is a fairly new feature in Magento, as Magento 1.x versions did not have functionality to automatically remove data from a module.
The Uninstall.php
class should implement the Magento\Framework\Setup\UninstallInterface
interface, which should contain the uninstall()
method.
Within this method, add code that drops your module’s database tables if they exist, remove any other data including configuration found within Stores -> Configuration
.
// vendor/[vendor_name]/[package_name]/Setup/Uninstall.php
<?php
namespace [Vendor]\[Module]\Setup;
use Magento\Framework\Model\AbstractModel;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\UninstallInterface;
use Magento\Config\Model\ResourceModel\Config\Data;
use Magento\Config\Model\ResourceModel\Config\Data\CollectionFactory;
class Uninstall implements UninstallInterface
{
/**
* @var CollectionFactory
*/
protected $collectionFactory;
/**
* @var Data
*/
protected $configResource;
public function __construct(
CollectionFactory $collectionFactory,
Data $configResource
)
{
$this->collectionFactory = $collectionFactory;
$this->configResource = $configResource;
}
public function uninstall(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
// Remove any module database tables if they exist
if ($setup->tableExists('vendor_module_table')) {
$setup->getConnection()->dropTable('vendor_module_table');
}
// Remove any store configuration that contains a path of 'vendor_module'
$collection = $this->collectionFactory->create()
->addPathFilter('vendor_module');
foreach ($collection as $config) {
$this->deleteConfig($config);
}
}
/**
* @param AbstractModel $config
* @throws \Exception
*/
protected function deleteConfig(AbstractModel $config)
{
$this->configResource->delete($config);
}
}
If you need to manually uninstall a module, ensure that you take a backup of the Magento codebase and database and check the module dependencies. Whilst having the maintenance flag up, manually follow the steps that the module:uninstall
command takes, and then take your website out of maintenance mode.
Note: This article is based on Magento Open Source version 2.1.8.