Uninstall Drupal 8 modules that includes default configuration

Parent Feed: 

In our modules we can include default configuration to ship content types, views, vocabularies on install. If we try to reinstall the module, we'll get an error. Let's take a look at why and how we can solve it.

Unable to install … already exist in active configuration

During uninstall the configuration is not removed because it has no connection to the module itself and thus we get the error because Drupal modules may not replace active configuration. The reason for that is to prevent configuration losses.

We can move all the configuration to config/optional rather than config/install which basically means that configuration that does not exists will be installed and the existing one will be ignored. If we do this we can reinstall the module.

So, if we want the configuration to remain active we've already solved the problem, but if we don't want that, we want the module to remove all configuration and contents its provided we'll need to look at another solution.

Let's take a look at the Article content type that is created when using the Standard distribution on installing Drupal.

node.type.article.yml

langcode: en  
status: true  
dependencies: {  }  
name: Article  
type: article  
description: 'Use <em>articles</em> for time-sensitive content like news, press releases or blog posts.'  
help: ''  
new_revision: true  
preview_mode: 1  
display_submitted: true  

field.field.node.article.body.yml

langcode: en  
status: true  
dependencies:  
  config:
    - field.storage.node.body
    - node.type.article
  module:
    - text
id: node.article.body  
field_name: body  
entity_type: node  
bundle: article  
label: Body  
description: ''  
required: false  
translatable: true  
default_value: {  }  
default_value_callback: ''  
settings:  
  display_summary: true
field_type: text_with_summary  

If we delete the content type, we can find out that the field instance body on the content type has been removed. First we actually see that the configuration will be removed when confirming the deletion of the content type, but also by looking in the database in the config table where active configuration is stored. It's gone.

The reason it's deleted is because it has an dependency on the node.type.article.yml configuration as we can see:

dependencies:  
  config:
    - field.storage.node.body
    - node.type.article
  module:
    - text

So what we need to do to make sure the content type we create when installing or module, that it's configuration uses our module as an dependency. So let's take a look at how we can do that:

Let's imagine we have a custom_event module that creates a event content type.

node.type.event.yml

langcode: en  
status: true  
dependencies:  
  enforced:
    module:
      - custom_event
third_party_settings: {}  
name: Event  
type: event  
description: 'Event'  
help: ''  
new_revision: true  
preview_mode: 1  
display_submitted: false  

The dependencies-part is the interesting one:

dependencies:  
    enforced:
        module:
            - custom_event

We have defined a custom_event module, in that module we have some exported configuration files in the config/install folder. We update the node.type.event.yml configuration file to have our module as an dependency. Now when we uninstall the module, the content type will be removed.

We also have to do this for our views, taxonomy, and field storages, or pretty much any configuration entity we provide configuration for. We don't have to worry about field instances, as we saw above those are dependent on the content type itself, but field storages on the other hand does not depend on a content type because you can reuse fields on multiple of those.

So, just add the module as an dependency and you're good to go, here's an example on a field storage field_image

field.storage.node.field_image.yml

langcode: en  
status: true  
dependencies:  
  enforced:
    module:
      - custom_event
  module:
    - file
    - image
    - node
id: node.field_image  
field_name: field_image  
entity_type: node  
type: image  
settings:  
  uri_scheme: public
  default_image:
    uuid: null
    alt: ''
    title: ''
    width: null
    height: null
  target_type: file
  display_field: false
  display_default: false
module: image  
locked: false  
cardinality: 1  
translatable: true  
indexes:  
  target_id:
    - target_id
persist_with_no_fields: false  
custom_storage: false  

Author: 
RSS Tags: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web