2

Taxonomy vocabularies are in the CMI as of Drupal 8, but I want to also keep the Taxonomy terms in code. I am programmatically creating terms in a custom module.

function taxonomy_vocab_install() {
  foreach (taxonomy_term_info() as $term_info) {
    $term = Term::create(array(
      'parent' => array(),
      'name' => $term_info['name'],
      'description' => [
        'value' => $term_info['description'],
        'format' => 'full_html',
      ],
        'vid' => 'department',
    ));
    $term->save();
  };
};

Now, I want to delete the terms on uninstall, but taxonomy_term_delete is depreciated. Any help on how to programmatically delete the created taxonomy terms in D8 when the module is uninstalled would be greatly appreciated.

function taxonomy_department_uninstall() {
  // ?? 
};

1 Answer 1

4

I did not test it myself, but as far as I see in the documentation, Term is using Entity::delete(). You might try to load the terms you wish to delete and call this method directly on them:

$term->delete();
2
  • 1
    Another option is to call the storage handler's delete method on an array of entity ids. I do not have any metrics about performance, but my gut feeling is that this would be faster.
    – mradcliffe
    Apr 14, 2016 at 20:45
  • 1
    Deleting multiple terms at once would definitely be faster, but not that $storage->delete() expects an array of entities, not ids.
    – Berdir
    Apr 14, 2016 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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