D8 Install/uninstall custom module - 'field_storage_config' entity with ID already exists I created a custom module which essentially is a replica of the Article CT .yml files which come with the D8 standard install-profile.

When I installed the custom module, it successfully created everything i expected it too. I then disabled it, then re-enabled and the module will not re-install giving the following error:


  Drupal\Core\Entity\EntityStorageException: 'field_storage_config'
  entity with ID 'node.custom_field' already exists.


I originally solved this issue by declaring the module as a Enforced dependency on the node.type.yml

langcode: en
status: true
dependencies:
   module:
    - menu_ui
   enforced:
     module:
       - custom_module


This worked for the body field and allowed me to enable/disable the module without getting the duplicate ID error. but as soon as i added the image field the ID is a duplicate and I cannot re-enable the module. The id is unique, and module will enable if i change the ID prior to enabling, but thats unacceptable.


  EDIT: appears after removing the image field from the CT, the body
  field now is now being affected by the same error. So enforcing the
  module doesnt work.


I would assume this is because it is inside of the config/install folder, but there are no examples on custom modules that install content types when installed. at least I cannot find any at the moment.

My question would be: What is the proper way to add content types when enabling a custom module?

how to avoid the duplicate ID error when installing content types when disabling/enabling the custom module?
Try putting the files in the config/optional directory either at first install or after uninstalling (because they will already exist). They should get installed even on first install if all dependencies are met. From the docs:


  
    For example, in the scenario that module A has optional configuration which requires module B, but module A is installed first and module B some time later, then module A's config/optional directory will be scanned at that time for newly met dependencies, and the configuration will be installed then. If module B is never installed, the optional configuration item will not be installed either.
  


https://www.drupal.org/docs/8/api/configuration-api/configuration-storage-in-drupal-8