I outline two methods for removing residual module files in Drupal 8.
You go to reinstall a module that you have previously uninstalled/deleted and you receive the message:
Unable to install module, module files already exist in active configuration.
What Drupal is telling you here is that there are still dormant files from the previous install within your database. You are attempting to create 'new' versions of these files by reinstalling the module but Drupal won’t comply because, as far as it’s concerned, the module is already installed. This is very frustrating because the module will not be listed in Extend and you no doubt have attempted to add and remove it several times from the admin interface and/or site file directory.
The obvious solution is to find these malingering files, delete them, and then install them again, this time with a 'clean' configuration where the module files are created anew. This is easier said than done however and often a simple Ctrl+F applied to your site’s file directory may not be sufficient to identify them.
There are two methods you can try. I recommend starting with the first and if this doesn’t work, move onto the second. The second method is the most fool-proof, however it carries greater risk as it requires you to interact with the database directly.
### 1. Configuration export
Obligatory back-up warning: remember to back-up your site and database before attempting either method!
We are going to download a complete configuration file, remove the dormant files and then re-upload the config. By working with what is effectively a clone of our module setup we reduce the risk of an error upsetting our live site components.
Navigate to Configuration → Development and select Configuration synchronization
Select 'Configuration synchronization'.
Select the Export pane and then click the Export button.
Export the configuration file.
This will download the configuration snapshot to your machine. Extract the file and then search for the files belonging to the module you are trying to reinstall, then delete them.
Locate the dormant module files and delete them. In our example we are deleting the files belonging to the 'Backup and Migrate' module.
Next, we need to re-upload the config, this time without the malingering files that were causing us trouble. The modified config file will need to be uploaded to Drupal in the same compressed format that we downloaded it in: tar.gz. If you don't know how to do this, please consult my article How to create and extract tar.gz files.
Then once again go to Configuration synchronization but this time select Import.
Import the config.tar.gz file you have created
Finally clear the Drupal cache and your browser cache (just to be sure) and attempt to reinstall the module in question. Hopefully it should install without a hitch.
So the first method didn't work? Don't worry, there is something else we can try. This requires us to operate directly on the live site however so we must be careful.
I would recommend backing up your site again (just to be on the safe side) and then also putting your site in maintenance mode for the duration of the fix. This will effectively make your site inaccessible to users for a short period.
To activate maintenance mode, navigate to Configuration → Development and select Maintenance mode.
Put your site in maintenence mode for the duration of the fix.
Next you will need to login to PHPMyAdmin either via CPanel or your local server set up. Once in, select your sites database and then click on the config tab. This will show you all of your sites module files.
Select and delete the files, turn off maintenance mode and clear the Drupal cache. Now when you re-install the module, there should be no problems.