Fix for "Could not delete temporary file '......' during garbage collection"

Submitted by Tom Thorp on Wednesday, October 26, 2016 - 22:44
Modified on Wednesday, August 1, 2018 - 02:57
Drupal 8 Logo
If you've ever maintained a Drupal website that has contained image files, you would of at some stage came across the following cron error message :
Could not delete temporary file "temporary://image.jpg" during garbage collection
Could not delete temporary file "temporary://image.jpg" during garbage collection


This error within Cron usually happens when the database is out of sync with the file system. The following scenarios can bring on this cron error within Drupal. 
  • Files/images been introduced in a multi-site configuration, however one sites' file directory has been misconfigured.
  • Files/images have been removed from a sites' directory structure without referencing it in the database. 
  • Permissions to files/images have been set incorrectly
References to files and images are stored within the 'file_managed' and 'file_usage' tables. To verify that this is the case, go into Views and select the 'Files' view. (If 'Show SQL Query' isn't enabled, go into the Settings tab with Views to enable it.) 

SQL Query

Go into /admin/content/files, then search for any temporary file that's failing the cron service within Drupal. Click on the column 'Used In' associated with that file. You will be presented with a screen like this :

SQL Count

On the address bar, you will notice a number at the end of the address line. This is the 'File ID' (or the field FID in the 'file_managed' & 'file_usage' tables) that tracks the physical location of the file. In this example, the file 'sql_query.jpg' exists in the database, but it does not exist in the file structure. Also, the file is not referenced on any entity. In this case, it would be a good candidate for the file-id entry to be removed from the Drupal database. 
To remove a selected file-id from the Drupal database, you can use either a MySQL GUI Admin tool, or do it via a MySQL command prompt. My preference is to do things via a MySQL command prompt, as it is often quicker and easier to do. You will need to log into a MySQL account that has at least 'delete access' privileges for this to work. 
mysql -uUser -pPwd
use drupaldb;
select * from file_managed; 
For each file_id which has the temporary file issue, run the following SQL command.
delete from file_managed where fid=fid#;
Exit out MySQL. Finally, go into /admin/config/development/performance and click on the 'Clear all caches' button. Run cron and check that there are no issues with any files/images been maintained.

About the author

Tom Thorp
Tom Thorp is an IT Consultant living in Miami on Queensland's Gold Coast. With over 30+ years working in the IT industry, Tom's experience is a broad canvas. The IT services Tom provides to his clients, includes :
Website development and hosting
Database Administration
Server Administration (Windows, Linux, Apple)
PABX Hosting and Administration
Helpdesk Support (end-user & technical).