Question

I'm a newbie in magento 2 development. I'm creating magento 2 module and testing with composer. My test project is in github. https://github.com/hussain-badusha/magento-badusha-test

In my module, i simply created a table in Setup/InstallSchema. As everybody does, i just want to delete my table when the user is uninstalling my module. So, after minutes of surfing, i found a way and used Setup/Uninstall to delete the table.

But, the table is not deleting while i uninstall the module

To reproduce the error, I installed it using composer by putting git url in repositories in magento installed root path. Then i used this command

composer require badusha/module-test @dev

To uninstall, i used this command

php bin/magento module:uninstall -r Badusha_Test

Uninstallation is successful but the table is not deleted.

Any help would be appreciable.

Thanks

Was it helpful?

Solution

i'm grateful to you all who helped me in fixing this one.

By the way, i found the exact problem.

In uninstall script, the getTableName function does not return table name with prefix. So the target table is not deleted because of incorrect table name.

To fix this issue, one need to replace the dropTable code

From

$connection->dropTable($connection->getTableName('your_table_name_here'));

To

$connection->dropTable($setup->getTable('your_table_name_here'));

Please note that you need to give $setup variable instead of $connection variable to get the table name with prefix.

That's it.

Once again, Thanks to all

OTHER TIPS

If you need to automatically remove table, created by module, add option --remove-data.

magento module:uninstall [--backup-code] [--backup-media] [--backup-db] [-r|--remove-data] [-c|--clear-static-content] \ {ModuleName} ... {ModuleName}

Follow this link: https://firebearstudio.com/blog/how-to-uninstall-magento-2-modules-and-restore-magento-2-backups-rollbacks.html

2nd Step

<?php

namespace Vendor\Module\Setup;

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

class Uninstall implements UninstallInterface
{
    /**
     * Module uninstall code
     *
     * @param SchemaSetupInterface $setup
     * @param ModuleContextInterface $context
     * @return void
     */
    public function uninstall(
        SchemaSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $setup->startSetup();
        $connection = $setup->getConnection();
        $connection->dropTable($connection->getTableName('your_table_name_here'));
        $setup->endSetup();
    }
}

But this script works only when module has been installed using the Composer:

Hence, it is recommended that for modules NOT installed via composer, manual clean up of the database and filesystem is necessary.

ref link : Is it possible to delete the table from the database when uninstall the module?

Hope this will help you.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top