5

We had some serious mistakes in a PDF uploaded to one of our websites. But after "deleting" it from Drupal, it is still accessible through direct links. Drupal doesn't seem to delete it on its own.

I still can find it in the table file_managed, but not in file_usage. Is it safe to delete the database row and file manually?

Already checked all the old questions here on SO but none of them works.

Thank you!

  • Did you try with flushing cache or run cron? I hope this will delete rows automatically because some module remove its deleted data after run cron or cache clear. – usmanjutt84 May 04 '18 at 11:58
  • Yeah I tried that too, but it looks like only temporary files will be deleted then. Files in table 'file_managed' with status 1 won't be deleted, even if they are unused. However, setting the status to 0 (temporary) will enable them for garbage collection. Unfortunately this is pretty impractical. –  May 04 '18 at 13:02
  • Duplicate of: https://drupal.stackexchange.com/questions/60392/how-to-remove-unused-files-from-directories – cweiske Sep 15 '21 at 06:35

4 Answers4

3

I know this is an old post, but I had the same problem and found no good answer.

So, after a little bit of trial an error, I came up with this:

<?php

// Some nice ideas from:
// https://stackoverflow.com/questions/52455491/how-to-set-file-usage-on-drupal-8

$file_storage = \Drupal::entityTypeManager()->getStorage('file');
$fids = Drupal::entityQuery('file')->condition('status', 1)->execute();
$file_usage = Drupal::service('file.usage');

foreach ($fids as $fid) {

  $file = Drupal\file\Entity\File::load($fid);
  $usage = $file_usage->listUsage($file);

  if (count ($usage) == 0) {

    $file->delete();

  }
}

Be careful as the code relies on Drupal reporting the correct number of nodes where the file is used, and that might not always be the case.

Balde Binos
  • 105
  • 7
3

We recently contributed a module Unmanaged / Unused Files | Manage | Delete

Dharman
  • 30,962
  • 25
  • 85
  • 135
1

You might want to try Fancy File Delete which has among other functionality the following options

Deleting unused files from the default files directory that are not in the file managed table. AKA deleting all the unmanaged files.

Deleting unused files from the whole install that are no longer attached to nodes & the file usage table. AKA deleting all the orphaned files.

Community
  • 1
  • 1
GiorgosK
  • 7,647
  • 2
  • 29
  • 26
1

You might wanna create a module that hook on media delete and suppress the associated file

use Drupal\Core\Entity\EntityInterface;
use Drupal\file\Entity\File;

function my_custom_module_entity_delete(EntityInterface $entity) {
  if ($entity->getEntityTypeId() === 'media') {
    $fid = $entity->getSource()->getSourceFieldValue($entity);
    $file = File::load($fid);        
    if ($file) {
      $file_usage = Drupal::service('file.usage');
      $usage = $file_usage->listUsage($file);
      if(count($usage) == 0) {
        $file->delete();
      }
    }
  }
 }
Matoeil
  • 6,851
  • 11
  • 54
  • 77