8.5.x entity.api.php hook_entity_delete(Drupal\Core\Entity\EntityInterface $entity)
8.0.x entity.api.php hook_entity_delete(Drupal\Core\Entity\EntityInterface $entity)
8.1.x entity.api.php hook_entity_delete(Drupal\Core\Entity\EntityInterface $entity)
8.2.x entity.api.php hook_entity_delete(Drupal\Core\Entity\EntityInterface $entity)
8.3.x entity.api.php hook_entity_delete(Drupal\Core\Entity\EntityInterface $entity)
8.4.x entity.api.php hook_entity_delete(Drupal\Core\Entity\EntityInterface $entity)
8.6.x entity.api.php hook_entity_delete(Drupal\Core\Entity\EntityInterface $entity)
7.x system.api.php hook_entity_delete($entity, $type)

Act on entities when deleted.

Parameters

$entity: The entity object.

$type: The type of entity being deleted (i.e. node, user, comment).

Related topics

1 function implements hook_entity_delete()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

entity_crud_hook_test_entity_delete in modules/simpletest/tests/entity_crud_hook_test.module
Implements hook_entity_delete().
8 invocations of hook_entity_delete()
comment_delete_multiple in modules/comment/comment.module
Delete comments and all their replies.
field_attach_delete in modules/field/field.attach.inc
Delete field data for an existing entity. This deletes all revisions of field data for the entity.
field_purge_data in modules/field/field.crud.inc
Purges the field data for a single field on a single pseudo-entity.
file_delete in includes/file.inc
Deletes a file and its database record.
node_delete_multiple in modules/node/node.module
Deletes multiple nodes.

... See full list

File

modules/system/system.api.php, line 371
Hooks provided by Drupal core and the System module.

Code

function hook_entity_delete($entity, $type) {

  // Delete the entity's entry from a fictional table of all entities.
  $info = entity_get_info($type);
  list($id) = entity_extract_ids($type, $entity);
  db_delete('example_entity')
    ->condition('type', $type)
    ->condition('id', $id)
    ->execute();
}

Comments

bobojo’s picture

Based on my experience with hook_entity_insert(), I'm going to assume that this function fires BEFORE the entity is actually deleted from the database, so any attempt to delete the entity from within this function will cause PDOExceptions. Seems like a weird and unlikely thing to attempt, but it's worth clarifying if that is indeed the case.

DamienMcKenna’s picture

Per node_delete_multiple(), this is triggered after hook_node_delete() but before either the fields or the base node records are removed.