Permanent Deleting of files still doesn't work for me (make_unused_managed_files_temporary = true)
Question
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).
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?
Solution
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 onlystatus=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
isempty()
this file is unused. Save all thatfid
id's into array$fids
, - SQL UPDATE; build update query to null these rows:
UPDATE file_managed SET status=0 WHERE fid IN($fids)
andUPDATE file_usage SET count=0 WHERE fid IN ($fids)
.
Finaly after next cron run the default function file_cron() {...
will properly delete these files.