Modify programmatically the 'core.extension.yml' file

Hello,

I have to modify the ‘core.extension.yml’ file programmatically, to delete a module from the list of modules.

To update I could do it in this way:

$extensions = Drupal::database()->select('config')
  ->fields('config', ['data'])
  ->condition('collection', '')
  ->condition('name', 'core.extension')
  ->execute()
  ->fetchField();
$extensions = unserialize($extensions);

$extensions['module']['MODULE_NAME'] = 0;

if (!isset($extensions['module']['MODULE_NAME'])) {

  Drupal::database()->update('config')
    ->fields([
      'data' => serialize($extensions),
    ])->condition('name', 'core.extension')->execute();

}

But when I want to delete the module from the list it doesn’t work.

What is the right way on doing this?

Thanks in advance:)

Drupal version: 


Source: https://www.drupal.org/taxonomy/term/4/feed