Modify programmatically the 'core.extension.yml' file


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')
$extensions = unserialize($extensions);

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

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

      '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: