3

I have created a custom module which is being installed via composer.

When I run the command - php bin/magento module:uninstall Vendor_Faq

It is removing all the corresponding tables which I wrote in Uninstall script. But how to remove the entry from core_config_data and setup_module table using Uninstall script.

3 Answers 3

2

from setup module it should be deleted automatically (might be wrong on this one), but from the config table you should delete them using the same uninstall script.

If the record from the setup_module table is not deleted automatically, you can do it from the same uninstall script.

If this is not your module then it means the uninstall script is not properly coded.

7

I think Marius has forgotten his own module :)

He has created a sample module with an uninstall script you can refer to same.

    $collection = $this->collectionFactory->create()
        ->addPathFilter('sample_news');
    foreach ($collection as $config) {
       $this->configResource->delete($config);
    }

Credit to Great Marius

2
  • I am confused whose answer to verify. You gave me the code which was written by Marius :)
    – Gagan
    Aug 22, 2018 at 11:10
  • Never mind hope you got the solution for your problem.
    – Priyank
    Aug 22, 2018 at 11:29
0

The answer shared by 2 is correct but just to update the code which worked for me -

There are 2 ways which I tried, and I am writing both the code here because both worked(There may be other ways also).

1) If you want to delete via delete method of Magento2 which is not so optimum way, but still want to share.

$connection->delete($connection->getTableName('core_config_data'),['path=?'=>'section/group/field']);

2) The link share by Priyank which has code by Marius -

use Magento\Framework\Model\AbstractModel;
use Magento\Framework\Setup\UninstallInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Config\Model\ResourceModel\Config\Data;
use Magento\Config\Model\ResourceModel\Config\Data\CollectionFactory;

public function __construct(
        CollectionFactory $collectionFactory,
        Data $configResource
    )
    {
        $this->collectionFactory = $collectionFactory;
        $this->configResource    = $configResource;
    }

In the public function Uninstall add the code

$collection = $this->collectionFactory->create()
            ->addPathFilter('section_id');
        foreach ($collection as $config) {
            $this->deleteConfig($config);
        }

Then add the function -

protected function deleteConfig(AbstractModel $config)
    {
        $this->configResource->delete($config);
    }

And Yes code from setup_module gets removed by itself

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.