EDIT: Solved. I'll leave this here for any future searchers.

Solution:
Drupal 8 has a setting to delete orphan images on the /admin/config/media/file-system page which it does as frequently as every 6 hours. Somehow the images in question below were designated as orphans by D8. There is a database table called "file_managed" which has a "status" column. A 0 in this column means Drupal thinks that image is an orphan and will be deleted if you have that option enabled on the admin page previously mentioned.

Somehow the entries for the images in question below were doubled in this table, and some of them had their status set to 0. I was able to resolve my cycle of frustration by re-uploading the images via FTP after a deletion cycle. It deleted the images, as usual, as well as the duplicate database entries which had a status of 0. Since all the entries were doubled for some reason and the ones remaining were believed not to be orphans (their status was 1), I just re-uploaded via FTP and Drupal left them alone.

I have googled like crazy and it seems like many people had this issue back in D7 but I'm on D8 and can't find anyone else experiencing this. I don't know if it's a problem with file field module or something else.

I have several Drupal installs on my server, only my D8 installs are having this issue. I'll give the scenario on both sites below what what I've done to investigate.

Site 1: Has file field type on my page content type, I attach the files and then use HTML a or img tags to link or insert into page. I have two pages where I've attached 1 file each, they're both images.

One of these images continually deletes off my server. It's there now, but sometime tomorrow or later today it will just disappear as if deleted from the server itself. When I edit the node it will still be listed, but the link to the file will be broken because its no longer on the server. It is gone from the server but Drupal still thinks its there. This continually happens to this 1 image, but the other image on a different page node is always there.

Site 2: Has a image field type on a "gallery" content type which just lists thumbnails of attached images which link to the full-size image. I have uploaded 20 images to a single node.

All of these images except 1 have the same behavior as above. When I go to edit the node they all still show as listed with broken thumbnails and links. They are physically gone from the server.

What I've tried
In both of these cases I have tried editing the node and clicking remove on each upload, saving the node, then re-editing and re-uploading the images and saving again. They again disappear within a day or so.

I've gone through this cycle about 5 times now.

Cause?
I can't correlate this behavior with anything. It's not happening when CRON runs which is what I was assuming since it seems to be so random and happens after I've stopped refreshing and leave it alone a while hoping its ok now.

I thought maybe it had something to do with how Drupal handles images, since I have site 2 setup to create thumbnails, but site 1 just treats them as file attachments and does nothing special to uploads.

It's not related to file type, it happens to both jpg and png file types, but not to all jpg and png files.

I've tried editing and re-saving the nodes with no changes, manually running cron, waiting for cron to run itself automatically, hard refreshing the pages, clearing cache and refreshing, doing so while logged out.

None of what I can think to do seems to cause the images to be deleted, but sure enough sometime after a day or so I'll check both sites and the images will be gone from the server but still show the data when editing the nodes.

I'm not running any custom code or modules, just very minimal installs with core modules (no experimental core modules). I have 1 or 2 downloaded modules on each site. Site 1: Google Analytics + Live Weather, Site 2: Disqus comments, so no common custom modules.

Any ideas?

Comments

Mackee’s picture

I have the same issue. Thanks for the solution. But on my end I just re-upload my image using a different file name.

donaldp’s picture

What I've found is that when you upload a file using a node edit form, ajax is used to upload the file. Whilst this is going on, the node save button is still enabled. If the node is saved before the file is fully uploaded, you can end up with duplicate entries in the file usage table for the uploaded file. One marked as permanent with a usage count of 1 and the other as temporary with a usage count of 0.
When the "temporary" file is deleted of course the permanent file disappears although the file is still referenced.

There seems to be two possible fixes for this.
1). Disable the node save button until the AJAX upload has completed.
2). The cleanup process should check for duplicate file names before removing the file.

This is not a fix for the problem but could give some help as to the cause? As the Ajax process can take some time, it is fairly likely that the node could be saved before the process has completed.

valvivian’s picture

I have the same issue with drupal 7 and having to keep reloading images on all the websites is so time consuming and unnecessary, so please someone fix this problem, because I have not found a solution and the above comments do not give a solution so this is not fixed.  

mikeytown2’s picture

valvivian’s picture

Hello,

This problem is very frustrating indeed.  For weeks I have been reloading images on all my websites only for them to disappear again or not resolve in browser.  I have applied the patch you suggested but the images are still not resolving some disappear altogether but some remain on the server but are not resolving.  I have tried reloading them with completely unique ids but within days the same things happen either they are not resolving or they have disappeared.  Please help.  Do you have any other patches for this problem and is anyone else experiencing this problem.

mikeytown2’s picture

Add this to your settings.php file.

// Make file_temporary_maximum_age 20 years.
$GLOBALS['conf']['file_temporary_maximum_age'] = 631138519;
xaxan’s picture

If u have some custom module, from where file uploads to server, also add this file_usage_add function. this function let Drupal know your module has a vested interest in this file and will not remove file at all