Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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

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

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