Exclude original cached external images from deletion during cleanup process

Created on 16 October 2025, 17 days ago

Problem/Motivation

Drupal provides a mechanism to periodically clean up cached external images — removing both derivative (styled) and original versions to free up disk space.

This is useful to avoid accumulating unused image files.

However, when full-page caching or external page caches (e.g., Varnish, CDN, or Drupal’s internal page cache) are in use, a problem occurs:

The original image from the external source is downloaded and cached when the content is first rendered.

Later, if the cached originals and derivatives are deleted by the cleanup process, and the HTML page remains served from cache, users will still request image style URLs that no longer exist.

When Drupal receives the request for the missing image style, it attempts to regenerate it using the original file — but since the original has been deleted, this regeneration fails.

The missing original will only be recreated once Drupal re-renders the content (i.e., when the page cache is invalidated). Until then, the images remain broken.

This leads to broken image derivatives on cached pages when original cached external images are removed prematurely.

Steps to reproduce

  • Enable image styles that use external image sources (e.g., via imagecache_external or similar system).
  • Enable page caching (Drupal internal cache or reverse proxy).
  • Visit a page containing external images styled by Drupal (e.g., using an image style).
  • The original image and its derivatives are now cached locally.
  • Run the image cleanup process that deletes cached originals and derivatives.
  • Request the same page again (still served from cache).

Result:

  • The HTML references cached image style URLs that no longer exist.
  • Drupal cannot regenerate derivatives because the original file is missing.
  • Images appear broken until the cache entry is invalidated and content is re-rendered.

Proposed resolution

Add a configuration option to the image cleanup process (or the relevant service/module) allowing administrators to exclude original cached external images from deletion while still removing derivative images.

Possible technical solutions:

  • Introduce a new configuration key in the cleanup service (e.g., delete_originals = TRUE/FALSE).
  • Modify the cleanup cron logic to check this setting before deleting original files.
  • Optionally, expose this setting in the UI under the image toolkit or file system configuration page.
  • Extend documentation to clarify how page caching interacts with image cache cleanup and potential side effects.

Benefits:

  • Prevents broken images on cached pages.
  • Maintains disk space optimization for derived images.
  • Provides better control to site administrators over cleanup behavior.

Remaining tasks

  • Identify the service or cron task responsible for cleaning cached images (e.g., image_cron() or module-specific cleanup function).
  • Add configuration schema and default value for the new setting.
  • Implement logic to skip deletion of original cached external images when the setting is enabled.

User interface changes

  • Add a checkbox under image cache or file system cleanup settings:
  • “Exclude original external images from cleanup (only remove derived image styles)”
  • Default: unchecked (maintains current behavior).
Feature request
Status

Active

Version

3.0

Component

Code

Created by

🇪🇸Spain lpeidro Madrid

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024