Fix for: Could not delete temporary file "temporary://filename.ext" during garbage collection

Submitted by Erik Wegner on
Body

A Drupal website needs a regularly running cron for several update and cleanup tasks. Once the file management taks yields the following message on each run:

Could not delete temporary file "temporary://favicon.png" during         [error]
garbage collection
Could not delete temporary file "temporary://logo64.png" during          [error]
garbage collection
Could not delete temporary file "temporary://logo96.png" during          [error]
garbage collection

The first thing to do is find the records within the database. For example, if your site uses a sqlite3 database, connect to it:

sqlite3 .ht.sqlite

Get a list of all tables:

.tables

You will see the table file_managed. That one is interesting. Since this site is a small one, there are not that much files. I can list them all on one page:

select * from file_managed;

Between all the records, there are the three in question:

37|83a446fc-e78f-4a22-bb34-5284f9161b6d|de|1|favicon.png|temporary://favicon.png|image/png|1159|0|1448487495|1448487495
38|920639fd-d910-4ec5-a132-508384d3996c|de|1|logo64.png|temporary://logo64.png|image/png|2018|0|1448487509|1448487509
39|b439cbf4-e84b-4bbf-ae44-68e2cb8ee7fe|de|1|logo96.png|temporary://logo96.png|image/png|2744|0|1448487619|1448487619

The first column is the file index column named fid.

A first attempt to remove the file directly through sql commands:

DELETE FROM file_managed WHERE fid = 37;

That brings the error:

Error: no such collation sequence: NOCASE_UTF8

Sqlite requires a special collation here which is provided by Drupal itself. If you do not want to write a custom module at this point, drush comes to your help. Fire it up and open a shell to your Drupal site:

/opt/drush8/drush core-cli

Now, all the Drupal API calls are at your fingertips. Especially this one is easy:

file_delete(37);
file_delete(38);
file_delete(39);

That's it. The next cron run is as quiet as expected.

Categories