14

I programmatically created a content type during a module installation (using field storage configuration).

I want to delete that content type while uninstalling the module.

Is there any way to do this with Drupal 8?

3 Answers 3

16

Just make sure that the node type depends on your your module, then Drupal will delete it automatically for you.

See node.type.book.yml in the book module for an example, this is the relevant part:

dependencies:
  enforced:
    module:
      - book

Note that users will have to delete all content of that type before they can uninstall the module then.

2
  • it seems to me that enforcing the module in node.type.custom.yml file is not sufficient to enforce the deletion of the nodes when uninstalling. Note that users will have to delete all content of that type before they can uninstall the module then. In my case the content type is deleted when uninstalling the module. But deletion of the custom content (nodes) is not enforced. To achieve this the custom module should implement ModuleUninstallValidatorInterface.
    – JoopSJ
    Commented Jul 24, 2016 at 20:23
  • Drupal core's book module is a good example of how to make sure that a module may not be uninstalled if there is still related content. A ProxyClass is also required if copying Book's services.yml file. In that case the following command will be found useful: php core/scripts/generate-proxy-class.php 'Drupal\custom_module\CustomModuleUninstallValidator' "modules/custom/custom_module/src"
    – vensires
    Commented Apr 6, 2020 at 23:16
11

This seems to do it for me.

$content_type = \Drupal::entityManager()->getStorage('node_type')->load('MACHINE_NAME_OF_TYPE');
$content_type->delete();
1
  • A CLI one-liner of this that uses Drupal Console with proper escaping is: drupal snippet --code='$content_type = \Drupal::entityManager()->getStorage("node_type")->load("MACHINE_NAME_OF_TYPE"); $content_type->delete();'
    – komlenic
    Commented Feb 13, 2020 at 20:06
4

To trigger some action upon uninstallation of a module you have to implement hook_uninstall in your module's *.install file. Prior to deleting the content type you might want to ensure all nodes of that content type got deleted as well. Finally, after uninstalling the module and having the content type deleted don't forget to export the updated config.

/**
 * Place a short description here. 
 */
function MYMODULE_uninstall() {

  // Delete all nodes of given content type.
  $storage_handler = \Drupal::entityTypeManager()
    ->getStorage('node');
  $nodes = $storage_handler->loadByProperties(['type' => 'MACHINE_NAME_OF_TYPE']);
  $storage_handler->delete($nodes);

  // Delete content type.
  $content_type = \Drupal::entityTypeManager()
    ->getStorage('node_type')
    ->load('MACHINE_NAME_OF_TYPE');
  $content_type->delete();
}
2
  • Will this delete any bundle of MACHINE_NAME_OF_TYPE, like the node-id query based answer does, or not? I mean, that seems to be somewhat expensive (if there are a lot of NIDs) and I would like to use this solution when possible.
    – Top-Master
    Commented Jun 29, 2019 at 11:13
  • 1
    @Top-Master – I updated my answer. It needs an extra step to delete all nodes first.
    – leymannx
    Commented Jun 29, 2019 at 21:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.