function File::preDelete

Same name in other branches
  1. 9 core/modules/file/src/Entity/File.php \Drupal\file\Entity\File::preDelete()
  2. 8.9.x core/modules/file/src/Entity/File.php \Drupal\file\Entity\File::preDelete()
  3. 10 core/modules/file/src/Entity/File.php \Drupal\file\Entity\File::preDelete()

Overrides EntityBase::preDelete

File

core/modules/file/src/Entity/File.php, line 197

Class

File
Defines the file entity class.

Namespace

Drupal\file\Entity

Code

public static function preDelete(EntityStorageInterface $storage, array $entities) {
    parent::preDelete($storage, $entities);
    foreach ($entities as $entity) {
        // Delete all remaining references to this file.
        $file_usage = \Drupal::service('file.usage')->listUsage($entity);
        if (!empty($file_usage)) {
            foreach ($file_usage as $module => $usage) {
                \Drupal::service('file.usage')->delete($entity, $module);
            }
        }
        // Delete the actual file. Failures due to invalid files and files that
        // were already deleted are logged to watchdog but ignored, the
        // corresponding file entity will be deleted.
        try {
            \Drupal::service('file_system')->delete($entity->getFileUri());
        } catch (FileException) {
            // Ignore and continue.
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.