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, the Media and File entity browser modules.

  • The answer from @Neograph734 is in theory correct, but you mention you use Media and File entity browser, is it now a file field or a media field? Media currently has no concept of automatically removing media entities or usage tracking, at least not without additional modules – Berdir May 22 '17 at 16:56
  • I should have mentioned it was just a file field. There was no need to mention the Media module as I don't do anything with it yet (I read that it is going into core of 8.4 so I won't fiddle with it too much). – Jedeco May 23 '17 at 14:24

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

Correct, however after editing, the status of the file should be set to 0. (Unless the same file is in use 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 example.com/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 release notes.

| improve this answer | |
  • 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 '17 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. – Neograph734 May 23 '17 at 15:40

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.

| improve this answer | |
  • 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 '18 at 9:30
  • Thnx for comment. I have included it into answer. – devad Feb 15 '18 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 '18 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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