Define custom delete behavior for this module's field data.
This hook is invoked from field_attach_delete() on the module that defines a field, during the process of deleting an entity object (node, taxonomy term, etc.). It is invoked just before the data for this field on the particular entity object is deleted from field storage. Only field modules that are storing or tracking information outside the standard field storage mechanism need to implement this hook.
\Drupal\Core\Entity\EntityInterface $entity: The entity for the operation.
$field: The field structure for the operation.
$instance: The instance structure for $field on $entity's bundle.
$langcode: The language associated with $items.
$items: $entity->{$field['field_name']}[$langcode], or an empty array if unset.
Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.
function hook_field_delete(\Drupal\Core\Entity\EntityInterface $entity, $field, $instance, $langcode, &$items) {
// Delete all file usages within this entity.
foreach ($items as $delta => $item) {
file_usage()
->delete(file_load($item['fid']), 'file', $entity
->entityType(), $entity
->id(), 0);
}
}