6

I have added a file attachment field to my basic pages.

Now for instance I want to delete a certain file from a page, how should I approach this?

I know deleting it from the file system isn't the correct way to go as it would still keep a database record of it.

However, if I remove it on the basic page 'Edit' page, it will also keep a record in the database.

How do I clean-up the database for files that don't exist or aren't used anymore?

I am using Drupal 8.

0

3 Answers 3

6

However if I remove it on the basic page 'Edit' page, it will also still keep a record in the database.

That's correct. However, after editing, the file status should be set to 0, unless the same file is used elsewhere. Drupal has a built in system that should automatically clean up these stale files both from the filesystem and the database, which you can configure on /admin/config/media/file-system. The default time is set to 6 hours (depending on when cron runs this could take up to the next cron run).

Be aware though that there are known issues with this automated deletion. See the File usage tracking causes unexpected file deletion section in the Drupal 8.3.2 release notes.

2
  • Thank you for the reply, I'm new to Drupal and still have a lot to learn. I find the way files are being handled a bit weird, but I suppose it's like that for large sites with lots of content and users. I'm developing a small site with basic pages and articles of which some have PDF file attachments. Deleting a file and reuploading it changes the name of the file with _0 at the end, even tough I deleted it from the node and from the filesystem. Is there a way to force this automatic clean-up instead of waiting for 6 hours to pass, perhaps with drush?
    – Jedeco
    May 23, 2017 at 14:38
  • I suppose the idea is indeed not to put load on the database and file-system during normal operations, so the site remains fast for the user. Changing this might make your site slow, but you can copy some logic from file_cron(), which is where the scheduled deletion is happening. Placing the code in a custom module by using hook_entity_presave(). Personally, I wouldn't bother. May 23, 2017 at 15:40
2

Contributed Drupal module Views Bulk Operations has initial D8 Alpha release which can help to delete orphaned file entities through UI.

Install the module, edit Files view, add "Views bulk operations" global field and configure it to include "Delete selected entities" action.

The module is currently limited from an Admin point of view as it only allows the person that uploaded the file to execute the delete. Trying to do it as anyone else (including user-1) will fail (with log error message). Hopefully this limitation will be fixed in releases to come.

3
  • This module is pretty useless from an Admin point of view as it only allows the person that uploaded the file to execute the delete. Trying to do it as anyone else will result in an error message.
    – John Cogan
    Feb 14, 2018 at 9:30
  • Thnx for comment. I have included it into answer.
    – devad
    Feb 15, 2018 at 13:53
  • 1
    BTW "Hopefully this limitation will be fixed in releases to come." The contributor of the module is not going to fix it as he has said that it is the way it is supposed to work.
    – John Cogan
    Feb 15, 2018 at 16:32
1

As of Drupal 10.1, it is now possible to properly delete files through the UI. You can do this by visiting the Files admin page.

See more at the Deleting Files documentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.