0

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

0

Did you try the --remove-data param?

https://devdocs.magento.com/guides/v2.3/install-gde/install/cli/install-cli-uninstall-mods.html

  • Yes i did try that too. But same result – Hussain Badusha Oct 5 at 11:56
0

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.

  • Yes i've already tried that method too using --remove-data parameter with module:uninstall command. But the custom table was not deleted while uninstallation was successful – Hussain Badusha Oct 7 at 15:13
  • code updated please check. Thank you – Ranjeet Kumar Oct 7 at 15:57
  • Thanks, i exactly did it in the way you are describing above. And yes i installed it via composer. Fyi, all filesystem, and setup_module row for my module, etc, are deleted automatically when i run module:uninstall command. The only problem i have is my custom table is not deleted. – Hussain Badusha Oct 8 at 4:48
0

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

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.