Hi,

We noticed that our images where slowly being deleted every time cron ran. I think I finally found why and would like to have your opinion.

We started with a content type using a field field_content_image for a bundle called article. We later decided to move this to a field collection field_collec_content_image. We manually moved data in the database to migrate existing content and deleted the obsolete field_content_image field. But we failed to delete the obsolete content of the field_data_field_content_image table.

I tracked down our problem to field_cron() which calls field_purge_batch($limit); This function looks at the tables field_config_instance, field_config for field instances that have been deleted.
In our case, it found that field_content_image was a deleted field for the bundle article. It then proceeds to delete field's data. I haven't gone through all the code but highly suspect it ends up deleting the file both in the file_managed, file_usage table and file system.

The reason I filled this as a bug is because I would expect any file delete to use the function file_usage_delete which would have picked up that the file was still in use.

Can you confirm that the field_purge_data function is really responsible for deleting the files ? If so, why is this not checking file usage before delete ?

Thanks

Comments

David_Rothstein’s picture

As far as I can tell from reading the code, field_purge_data() ultimately calls file_field_delete_file() which decrements the file usage count by 1 and then attempts to delete the file. The deletion should only succeed if that one reference (that it deleted) was the only reference, i.e. if there are no references remaining.

If you moved stuff around manually in the database but never added anything to the {file_usage} table (to record that the new field collection is now referencing the file also) then there would only be a single reference in the {file_usage} table, from the deleted field... and therefore the system would believe the file is no longer in use and safe to delete.

jbenezech’s picture

Thanks David for your comment. The file_usage counter had been updated and was actually set to 2 on the few files I checked. I'll have a closer look and do some more tests.

dcam’s picture

Category: Bug report » Support request
Status: Active » Closed (fixed)
Issue tags: -cron, -field

What @David_Rothstein said in #1 sounds correct to me. Since there was no additional reply from the OP, I'm going to assume the problem was resolved and close the issue. Please feel free to re-open it if not.