Code that can be used to permanently delete unused/orphaned files in drupal 9 and 8.
// get all files ids
$fids = \Drupal::entityQuery('file')->execute();
$file_usage = \Drupal::service('file.usage');
// loop all fids and load files by fid
foreach ($fids as $fid) {
$file = \Drupal\file\Entity\File::load($fid);
$usage = $file_usage->listUsage($file);
// check if file not used
if (count($usage) == 0) {
$file->delete();
}
}
Warta Lab is one of the best websites for tutorials on PHP, Drupal, Drupal 8, and general programming topics. Visit us for expert solutions, in-depth guides, and valuable programming knowledge
Drupal 8 and 9 delete file programmatically
Subscribe to:
Post Comments (Atom)
Write a program in PHP to reverse a number
A number can be written in reverse order. For example 12345 = 54321 <?php $ num = 23456; $ revnum = 0; while ($ num > 1) { $...
-
General Drupal Knowledge What are the key differences between Drupal 9 and Drupal 10? Click Here What are some of the major improvements i...
-
Drupal 10 builds upon the Drupal 9 but introduces several key changes and improvements. Below are the main differences: 1. Symfony Upgrade ...
No comments:
Post a Comment