5

I have trouble deleting Files on Drupal 8. How can you delete any kind of Files for example in my case a picture file (jpg, png etc...). In Drupal 7, you have the option on the right side to Delete the file , but Drupal 8 does not have that option ...only for published pages. Is ther any chance to delete files just for any reason if you do not want them any more, without any complicated task ?

P.S: I am a beginner Drupal user ! Thanks

attila.hajdo
  • 79
  • 1
  • 10

3 Answers3

2

In Drupal 8 there is no need of that option, since there is cron which will be running automatically to delete unused images.

  • I have runed Crone manually but nothing happens. Does it need some time to pass or something? Or what if i want to keep a file for later work, but is not used for days in any places ? – attila.hajdo Jul 02 '18 at 07:04
  • 2
    If the files are older than 6 hours it will be deleted when running cron manually.. – Sathish Sundar Jul 02 '18 at 12:15
  • Cron only removes files that are marked "temporary", not "permanent" files. Any files that were marked used by "file.usage" are "permanent" files. Even if the file is removed from a node and has zero uses, it will not be removed. – mbomb007 Sep 18 '20 at 13:49
2

There is no native way from the Drupal 8 admin interface to delete images from the file uploader in Drupal. However you can install the Drupal 8 IMCE module which will give you access to the file system when you are login. That way you can remove files.

Here is a link to the module. https://www.drupal.org/project/imce

0

You can erase images from a custom module. This is only tested in Drupal 8.9. First let me explain the image process so you can better understand if this solution is the right fit for you. An fid is stored in the column of your custom table you use to reference your image. It is the primary key in a table called file_managed that stores the image information. The image is stored in the "web/sites/default/files" folder. This might be different depending on your Drupal installation. table_usage stores the usages of the image on the site. When you erase an image you need to erase the row with your images fid from all three tables file_managed, file_usage and your table. Then of course unlink your image.

    /**
 * Removes an image.
*
* @param $fid
* @return boolean
*/
public function removeImage($fid) {
    if ( empty($fid) || !preg_match('/^[0-9]+$/', trim($fid)) ) return FALSE;

    // Im assigning the $this->database property an instance of Connection
    $result = $this->database->query("SELECT uri FROM {file_managed} where fid=:fid", [':fid' => $fid])->fetchCol();

    $absolute_path = \Drupal::service('file_system')->realpath($result[0]);
    if(isset($absolute_path)) unlink($absolute_path);
    //var_dump($absolute_path); exit();

    $this->database->delete('file_managed')
        ->condition('fid', $fid)
        ->execute(); 
        
    $this->database->delete('file_usage')
        ->condition('fid', $fid)
        ->execute();

    return true;
}
Trigve Hagen
  • 1
  • 1
  • 4