I have created a custom module and created a yml file to create a menu

system.menu.custom_menu.yml

id: custom_menu
label: 'Custom menu'
description: 'Custom menu.'
langcode: en
locked: true
dependencies:
  - mymod

When I installed my module it creates the the custom menu. But then When I uninstall the module and try to install it again I get the following exception

exception 'Drupal\Core\Config\PreExistingConfigException' with message 'Configuration objects (system.menu.custom_menu) provided by mymod already exist in active configuration' in
/var/www/drupal8/core/lib/Drupal/Core/Config/PreExistingConfigException.php:70

What I am doing wrong? Is there any way to remove my configuration (system.menu.custom_menu) from the configurations when I uninstall my module?

Comments

qqboy’s picture

i guess in d7 there is a module hook

function hook_module_uninstall(){

}

But no idea about d8....sorry.

slewazimuth’s picture

All configuration files are stored in the Drupal 8 database "config" table. You can delete your "left over" configuration file there by looking it up in the table and manually deleting it which will allow you reinstall your module. If your module is not uninstalling correctly you can also uninstall it with Drush. When you choose the Uninstall tab from the Extend menu you should get a prompt advising you that uninstalling the module will delete the "config" files for the module. However, various things can happen during module development which prevent a clean uninstall.

heykarthikwithu’s picture

Implement a hook_uninstall() in your modules install file

to delete a leftover schema you can use following command.
drupal_uninstall_schema('link_click_count');

to leftover views you can delete with the help of view machine name.

 db_delete('config')->condition('name', 'views.view.link_click_count')->execute();

OR
\Drupal::configFactory()->getEditable('views.view.link_click_count')->delete();

rakesh.gectcr’s picture

Thanks @ heykarthikwithu! that works fine for me.

// Deleting the views while uninstalling.
 \Drupal::configFactory()->getEditable('views.view.scheduled_content')->delete();

Or You can use the Following code as well

$entityView = \Drupal::entityTypeManager()->getStorage('view')->load('test_view_to_delete');
    $entityView->delete();

---
Kind regards,

Thank you,
Rakesh James

colan’s picture

See How to remove mymodule configurations on uninstall for a best-practice solution.

Basically, you can enforce the dependency on your module in your configuration files. So when the module is uninstalled, it's associated configuration will be uninstalled as well.

jericho711’s picture

You must set your custom module as a dependency in your YML file by setting "enforced" under dependencies.

For example:

dependencies:
  module:
    - node
  enforced:
    module:
      - custom_module
arnoldbird’s picture

[deleted]

schiavone’s picture

Using this after uninstalling the module I was not able to enable it because it is now dependent on itself. You should be fine without this unless you have submodules in which case defining dependencies may be useful. For some cases you may have to use hook_uninstall in the yourmodule.install file.

juancarielo’s picture

still working, thanks!

jayemel’s picture

None here seems like a viable option for an instance with multiple config files. For example, my migration in development that has 10+ migrate_plus config files. I'm kind of in horror over what struggle it is developing this migration incrementally. i.e. having to reimport configs every time I make a change to a migrate file... and the need to delete them all somehow when things go awry. Not a good time.

socialnicheguru’s picture

How do you uninstall a plugin?

http://SocialNicheGuru.com
Delivering inSITE(TM), we empower you to deliver the right product and the right message to the right NICHE at the right time across all product, marketing, and sales channels.

vishal.sirsodiya’s picture

Could you use hook_uninstall().

/**
 * Implements hook_uninstall().
 */
function hook_uninstall() {
  \Drupal::configFactory()->getEditable('system.site')->clear('remove_var_name')->save();

Thanks

gaurav.kumar’s picture

Get the right dependency in .yml file like in node.type.content_type_name.yml or field.storage.node___.yml files

dependencies:
enforced:
   module:
     - foobaryourmodulename