Uninstall module with database table schema using Magento 2.

Uninstall module using Magento 2, Native command is module:uninstall to remove module from the system.

Uninstall command works only with a module installed using a composer or defined as Composer packages.

Full Command with options:

bin/magento module:uninstall {ModulePackage_ModuleName} [–backup-code] [–backup-media] [–backup-db] [-r|–remove-data] [-c|–clear-static-content]

Using the above command with a manually installed module, Module will be not removed from the system neither module database table removed,

You have to remove it via manually delete the module folder from app/code or using shell command rm -rf {Modulename}.

A database table of the module will be removed only if your module contains composer dependency.

How Can We Delete the Database table and module folder using Command?

    • Prerequisite,
      Module Defined as Composer packages. You can verify your module name in the root composer.json file, require {} section.
      Example,

      "require": {
          "jesadiya/module-hello-world": "^1.0",
      }
      

      Module Remove the database table data and schema only, if you have specified –remove-data option with command.

    • If –remove-data is specified with the command, it removes the database schema and data defined in the module’s Uninstall class under the Setup folder.
  • bin/magento module:uninstall Jesadiya_HelloWorld –remove-data

Above command remove database table plus code of the module.

You need to create Uninstall Class to remove the module database table entity.

Uninstall class implements Magento\Framework\Setup\UninstallInterface.

Example Using Setup Folder Uninstall.php file,

<?php declare(strict_types=1);

namespace Jesadiya\HelloWorld\Setup;

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

class Uninstall implements UninstallInterface
{
    /**
     * Remove data that was created during module installation.
     *
     * @param SchemaSetupInterface   $installer
     * @param ModuleContextInterface $context
     *
     * @return void
     */
    public function uninstall(SchemaSetupInterface $installer, ModuleContextInterface $context): void
    {
        $installer->startSetup();

        $installer->getConnection()->dropTable($installer->getTable(MODULE_TABLE_NAME_1));
        $installer->getConnection()->dropTable($installer->getTable(MODULE_TABLE_NAME_2));
        $installer->getConnection()->dropTable($installer->getTable(MODULE_TABLE_NAME_3));

        $installer->endSetup();
    }
}

Here, Module contains 3 database tables at installation time, You need to remove all three database table at uninstall time using Uninstall class.

Replace your table name with MODULE_TABLE_NAME_1, MODULE_TABLE_NAME_2 and MODULE_TABLE_NAME_3 in the above script.

Keep In Mind, Uninstall Command will work only for Module defined as Composer Package.

Check the Magento Core Module example from Vendor/Temando_Shipping Module.