Deleting the translated nodes is leaving (1) orphan file(s)

Created on 15 November 2022, over 1 year ago
Updated 21 February 2023, over 1 year ago

Problem/Motivation

Translated node file entities are not getting deleted

Steps to reproduce

1. Create a File content type with fields - Title, Language, File and Enable translations
2. Create a file node with English language - Add the values for fields - Title, Language, File - (Attach a english language pdf - testEnglish.pdf)
3. Add translations for Any language 'french' . Click on Translate, Add data for the fields , attach a french pdf - testFrench.pdf
4. Add another trnaltion record for other language. Add file for that language eg: testGerman.pdf
5. Now go to Files tab under Admin > Content > Files, Check the usage count : it is showing below count
testEnglish.pdf - 3 places
testFrench.pdf - 2places
testGerman.pdf - 1 places used
6. Now delete the Node for german language, clear cache, run cron and check the Admin > Content > Files, it is showing below count:
testEnglish.pdf - 3 places
testFrench.pdf - 2places
testGerman.pdf - 0 places used
7. Now delete the Node for French language, clear cache, run cron and check the Admin > Content > Files, it is showing below count:
testEnglish.pdf - 3 places
testFrench.pdf - 1places
testGerman.pdf - 0 places used
8. Now delete the English language node , clear cache, run cron and check the Admin > Content > Files,it is showing below count:
testEnglish.pdf - 0 places
testFrench.pdf - 1places
testGerman.pdf - 0 places used
9. On checking the file_managed and fileusage tables the records show status of the file as 1 and entity label is blank as the node for french was deleted.
10. The file testFrench.pdf - 1places is left orphaned as the node associated with it is deleted but still its not marked as 'temporary' and file status is also '1'
11. Everytime we create translations its leaving one file as orphan, as the add translation is adding the file usage count as two for first translated node entity.

Note: We have added the $config['file.settings']['make_unused_managed_files_temporary'] = TRUE; in settings file.

Proposed resolution

We have tried the file delete modules but the file is not getting deleted as the staus is active and there is dependency on the file.
We may have to add a custom hook or module to clean the orphan files.
Updating custom code which can be added on hook cron :

	// Fetch all the orphan files
	$database = \Drupal::database();
	$query = $database->query("SELECT fid FROM {file_usage} WHERE fid IN (SELECT fid FROM (SELECT fu.fid  as fid FROM {file_usage} fu LEFT JOIN {node} n ON n.nid = fu.id WHERE n.nid IS NULL and fu.type =:ids_1 AND fu.count <>:ids_2) as tt)",[":ids_1" => 'node', ":ids_2" => 0]); 
	$resultfids = $query->fetchCol();
	$file_usage = Drupal::service('file.usage');
	  foreach ($resultfids as $fid) {
		$file = Drupal\file\Entity\File::load($fid);
		if($file){
		$usage = $file_usage->listUsage($file);
			if (count($usage) <> 0){
				//Delete Orphan files
				$filename = $file->getFilename();
				$url = $file->getFileUri();
				$file->delete();
				 \Drupal::messenger()->addMessage('Orphan files deleted successfully');
				}else{
		\Drupal::messenger()->addMessage('No Orphan files');
					}
			}	   
		}

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

πŸ› Bug report
Status

Fixed

Version

9.5

Component
File systemΒ  β†’

Last updated about 12 hours ago

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • The issue still exists in latest 9.5.3 Drupal version https://www.drupal.org/project/drupal/releases/9.5.3 β†’ as well. I have written a custom code on cron run to clear all the orphan files. Its working as expected.

    	// Fetch all the orphan files
    	$database = \Drupal::database();
    	$query = $database->query("SELECT fid FROM {file_usage} WHERE fid IN (SELECT fid FROM (SELECT fu.fid  as fid FROM {file_usage} fu LEFT JOIN {node} n ON n.nid = fu.id WHERE n.nid IS NULL and fu.type =:ids_1 AND fu.count <>:ids_2) as tt)",[":ids_1" => 'node', ":ids_2" => 0]); 
    	$resultfids = $query->fetchCol();
    	$file_usage = Drupal::service('file.usage');
    	  foreach ($resultfids as $fid) {
    		$file = Drupal\file\Entity\File::load($fid);
    		if($file){
    		$usage = $file_usage->listUsage($file);
    			if (count($usage) <> 0){
    				//Delete Orphan files
    				$filename = $file->getFilename();
    				$url = $file->getFileUri();
    				$file->delete();
    				 \Drupal::messenger()->addMessage('Orphan files deleted successfully');
    				}else{
    		\Drupal::messenger()->addMessage('No Orphan files');
    					}
    			}	   
    		}
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.69.0 2024