Can a module install a content type optionally or conditionally? I have a module which installs a number of different custom content types, each defined in a .yml file in config/install/.

I install my module, and the content type appears and works normally, and I used them to create content.

I was doing some other development that required me to uninstall and reinstall this module. It uninstalled with no problem, and I did not lose any of the content built on the module's content types.

However, the module could not be reinstalled. When I tried, I got the error message:


  Unable to install CustomModule, node.type.custom_node_type_one, node.type.custom_node_type_two already exist in active configuration.


where custom_node_type_one and custom_node_type_two are the content types installed by my theme.

This seems like a potentially serious problem. My module defines blocks that appear on nodes using these custom content types, but they also may contain independent content. If the module is uninstalled for any reason, I don't want to force the site admin to wipe all of that content--but I also don't want the site to be blocked from reinstalling the module.

Is there a way in Drupal 8 to make the installation of a content type conditional, so that the module can be uninstalled and reinstalled without having to first delete all of the site's custom content?
Don't install a module when its default configuration has unmet dependencies adds a config/optional folder, which silently ignores configuration that already exists.

config/optional can contain configuration that will only be installed if the required dependencies of those config entities are met, for example a default view that would only be installed if views is enabled and is not required for the module.