0

I've got a bizarre problem with a website I'm working on.

Recently, when anyone uploads images to the site, some time later (maybe an hour, sometimes several hours) one or more images disappear from the server. They still show up in the Media Library, and all the other sizes created by Wordpress are still on the server. But whatever size is being used on the page (usually the full size image, but not always) randomly gets deleted.

If I replace the missing file via FTP, they tend to disappear again fairly quickly (sometimes minutes, sometimes hours later).

I've tried removing any plugins recently installed. No luck.

I've even migrated the site to a completely different server. Oddly, it seemed to work at first. For several days, I had the site on the new server and no images disappeared. But then, once I updated the DNS settings to point the domain over, then they started to disappear.

Neither hosting company had any clue what could be causing it.

Have you ever seen a problem like this? Any idea what function I can look for that could be deleting images at random?

2

Obviously, I imagine you already checked cron jobs.

No one can tell you, but whenever you have an issue and you're wondering "what the hell is interacting with my system?" and that part that's problematic (in your case, the deletion of the image) has actions inside of it, we can see what called that specific deletion. So let's see how this applies in your case.

An image is a post. When it gets deleted, unless something bad happens, you can safely say that wp_delete_post will be used and inside of this function, we have the earliest action we can hook into before_delete_post:

add_action( 'before_delete_post', function( $postid ) {
    $past_backtrace = get_option( 'backtrace_for_deletion', [] );
    $current_backtrace = debug_backtrace();

    $past_backtrace[] = json_encode( $current_backtrace );
    update_option( 'backtrace_for_deletion', $past_backtrace );
});

We basically want to see what called this deletion, so let's make it a bit prettier:

    $backtraces = get_option( 'backtrace_for_deletion' );
    foreach( $backtraces as $backtrace ) {
        ?>
        <pre>
        <?php
            print_r( json_decode( $backtrace ) );
            echo '-----------------------------------------------------------------------------------';
        ?>
        </pre>
        <?php
    }

This isn't exactly the best view, but it gives you the information.

A few points:

  1. Third item is always where the deletion is called from, it'll give you the exact file name and line of what called the deletion.
  2. If this doesn't work, then I advise you turn server file logging on, as well as database logging and look at the queries.
  • Thanks for this. I dropped that code into my functions.php file and I saw it's output many times when I updated plugins, etc. However, since I posted this question, the problem seems to have gone away on its own, so I never got to see any output related to the deleted image files. But I'll accept this answer since it seems to be the correct way to go about solving the problem. – thingEvery Jun 17 at 17:59
  • 1
    The moment it happens, if you're running this code, you have the backtrace available and ready for you to see what actually impacted that deletion. – coolpasta Jun 17 at 20:31
0

If it occurs periodically, the first place I would check is scheduled tasks. I would also check your error log to see if anything shows up there. I would also check permissions.

Of course, if you can't figured it out, try disabling all plugins and changing the theme.

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.