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