Clean up files that are not used by upload module or filefield fields

GiorgosK - October 27, 2009 - 15:57
Project:Audit Files
Version:6.x-3.x-dev
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:active
Description

There are case where filefield uploads will end up with an AHAH error and even though the download has been completed and a file has been recorded in the {files} database the file is not referenced from anywhere
see an example #434394: 'HTTP error 0 occurred' on image upload

in such cases lots of orphan files are in the database and in the files directory ...

I checked for reference of the fid in all filefield field tables and the upload
and there was no reference I would erase the record from the {files} table

here is the mySQL to see the ORPHANS

SELECT `files`. *
FROM `files`
WHERE NOT EXISTS (
SELECT * FROM `content_field_images`
WHERE `content_field_images`.`field_images_fid` = `files`.`fid`
) AND NOT EXISTS (
SELECT * FROM `content_type_banner`
WHERE `content_type_banner`.`field_banner_fid` = `files`.`fid`
) AND NOT EXISTS (
SELECT * FROM `upload`
WHERE `upload`.`fid` = `files`.`fid`
)
LIMIT 0 , 1000

and here is how I deleted them

DELETE
FROM `files`
WHERE NOT EXISTS (
SELECT * FROM `content_field_images`
WHERE `content_field_images`.`field_images_fid` = `files`.`fid`
) AND NOT EXISTS (
SELECT * FROM `content_type_banner`
WHERE `content_type_banner`.`field_banner_fid` = `files`.`fid`
) AND NOT EXISTS (
SELECT * FROM `upload`
WHERE `upload`.`fid` = `files`.`fid`
)

and then let auditfiles find the ORPHAN files in the directory and erased them

I am not sure if the previous applies to all possible scenarios
(CAN files be referenced from other places ??)
but maybe it can be generalized and put into code

NOTE: finding the filefield tables seems to be as easy as looking for the _fid at the end of the fields of the content_ tables, but maybe I am mistaken

#1

giorgio79 - February 14, 2010 - 16:30

+1

 
 

Drupal is a registered trademark of Dries Buytaert.