make_unused_managed_files_temporary didn't work for me.

I am using Drupal backend for the REST API frontend application.

Thats how the File input looks (because I am not sure if that setting is for that File elements, but guess so).

How file field looks

I read some informations about permanent files deleting, because I would like to delete files permanent from the file system and also database.

I was set the make_unused_managed_files_temporary to true and debuging, this is really true in the code.

I also found the configuration in Backend / File system - Delete temporary files after (6 hours), so this is on.

I am checking also the database file_managed before and after (and after 6 hours) file deletion for the flags like status that indicates isTemporary() method. But after uploading file, saving... then editing, and removing file, the file is still in the database as a status 1.

I am also checking DB table file_usage where there is still the item with count=1 too, that should be not true (because of deletion).

And files maybe should move to /tmp folder, but that doesnt. I am working on Drupal 8.5.6 and 8.7.2 on production, but the behaviour is the same. I am doing something wrong? Is there some cron to be configured? Or another futher settings?

Edit 1: I think it is not a Media, but simply file widget - /core/module/file

Edit 2: The main problem is that, when I delete file, it stay in the database as a permanent flag (status=1), and also it has a count of usage 1 (file_usage=1). When I manually update database to set status=0 and count=0, the cron works OK. Why the file usage and count stay on?

  • Can it be that cron actually dumps the garbage collection in the end and you have no cron set up?
    – leymannx
    Commented Feb 8, 2021 at 20:22
  • 1
    Found the comment "apparently existed Permanent file with already 0 usage won't be changed to Temporary. That files need manually removed" and a link to drupal.org/project/drupal/issues/2821423 on drupal.org/node/2891902
    – leymannx
    Commented Feb 9, 2021 at 10:24
  • Okay, so how do you remove it then? What code? Can you add that to your question? Sounds you should maybe delete them properly calling some CRUD function and not simply remove them?!
    – leymannx
    Commented Feb 9, 2021 at 12:09
  • 1
    IIRC unused files will be deleted after multiple cron runs. Wait for few crons to run ? Commented Feb 9, 2021 at 15:25
  • Thanks for your answers mates, but I have a new result, see the Edit 2.
    – Maxo
    Commented Feb 10, 2021 at 9:12

1 Answer 1


I just made a solution that works for me even the Drupal or config doesn't works properly. In my plugin I hooked up the hook_cron() for logging these unused files to proceed them next.

  • SQL SELECT; query to fetch all file_managed fid's. You can select only status=1, as permanent files, but I got all,
  • PHP loop all id's and making a Drupal\file\FileInterface entity reference $file = File::load($item->fid) to check all the file references $references = file_get_file_references($file). When the $references is empty() this file is unused. Save all that fid id's into array $fids,
  • SQL UPDATE; build update query to null these rows: UPDATE file_managed SET status=0 WHERE fid IN($fids) and UPDATE file_usage SET count=0 WHERE fid IN ($fids).

Finaly after next cron run the default function file_cron() {... will properly delete these files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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