Skip to content
Maintenance & updates

Cleaning Up Unused Shop Media: Storage and Backups

Shopware removes unused media with media:delete-unused. How the dry run, grace period and backups fit together, and which images the cleanup run misses.

19 min read ShopwareMedienSpeicherplatzBackup

In short and in plain language

Shops often keep pictures that no one needs anymore. Old sale banners are one example. Copies of the same photo are another. They take up space, and they are in every backup.

Shopware has a command for this. It looks for pictures that are not used in the shop. In a test run, it only shows a list. It leaves new pictures from the last 20 days alone.

But the command does not see everything. Some add-ons save pictures in their own way. Then the add-on has to report its pictures. If it does not, the command deletes them.

A deleted picture is missing from every new backup. So you back up everything before you delete. Pictures for next year go into an archive.

Only the list should run on its own. A person checks the list. Only then are files deleted.

After Black Week and Cyber Monday, many media libraries hold more than the shop still shows: sale banners nobody sees once the sale has ended, newsletter headers, campaign images with prices that no longer apply, and product photos that got a new name when someone uploaded them a second time. Every one of these files takes up space on the server, sits in every backup and remains reachable at its address. For the cleanup, Shopware ships the command media:delete-unused, with a dry run and a grace period of 20 days by default for fresh uploads (Shopware documentation). The command only recognizes the usages it knows about, though. Images that an extension keeps in its own configuration, or that only appear as an address in a description, are invisible to it. And once a file is deleted, it is missing from every later backup (BSI implementation notes CON.3). This article explains what Shopware treats as unused, how to read the dry run and the report, which exceptions extensions have to report, and in which order backup and deletion belong together. It is based on the Shopware documentation, the changelog and the source code of version 6.7.16.0 (GitHub).

Key takeaways

  • With media:delete-unused, Shopware deletes all media that no other record references. Files uploaded within the last 20 days are spared by default (Shopware documentation).
  • Shopping Experiences, custom fields of type media and the theme configuration are checked by the core through an event. Images in custom elements of extensions or as an address in text are not recognized (Shopware source code).
  • Every deletion run is preceded by a dry run with --dry-run or a list with --report. It shows file name, title, upload date and size and goes to marketing and editorial staff for approval (Shopware source code).
  • A deleted file is missing from all later backups (BSI implementation notes CON.3). If the oldest generation goes back 6 months, as with the backup service for shops on our servers, a seasonal image needs a snapshot of its own.
  • Without interaction, for example in a cron job, the command deletes without asking (Shopware source code). Only the report should run automatically; deletion follows approval, backup and a thumbnail check in the maintenance window.

What piles up in the media library after Black Week

A sale week leaves traces in the media library that nobody sees in the shop anymore. Banners for the home page and categories are taken out of the Shopping Experiences after Cyber Monday, but the files stay. Images uploaded for newsletters and ads were not assigned to any product in the first place. Then there are duplicates: when someone uploads an image that already exists, the Administration offers to replace the existing file, upload the new one under a different name or use the existing file (Shopware documentation). Anyone who renames instead of replacing in the rush before a sale ends up with two files, only one of which is in use. And when a product image is swapped, the old image stays in the media library as a media item, just without an assignment. The Shopware documentation describes the consequence itself: unused media can take up a lot of storage space and make the media overview confusing (Shopware documentation).

Each of these files costs more than once. In the settings of every folder, Shopware defines the sizes in which thumbnails are generated; the documentation lists three default sizes of 400x400, 800x800 and 1920x1920 pixels (Shopware documentation). For the product media folder, a migration adds another size of 280x280 pixels since Shopware 6.5 (Shopware source code). In a standard installation, a product image can therefore exist up to five times on disk: as the original and in four thumbnail sizes. All of these files go into every backup. The BSI describes the relationship plainly: the higher the data volume, the more storage is needed and the higher the cost of a generation, and a high data volume can limit the number of generations for economic reasons (BSI implementation notes CON.3). Backing up unused media therefore costs not only storage but potentially shorter retention. Which other directories grow unnoticed on the server is covered in the article on shop disk space.

Unused does not mean unreachable

Product images and media files live in the public filesystem, which Shopware runs with public visibility; in the default storage these are the directories public/media and public/thumbnail (Shopware documentation). A file without a usage stays reachable at its address for as long as it exists. For a campaign image showing a price that no longer applies, that is more than a storage question.

What Shopware treats as unused

According to the commands reference, media:delete-unused deletes all media files that are not used anywhere (Shopware documentation). What used means is defined in the developer guide: a media item counts as unused when no other record references it, and that works well as long as all references are stored as proper foreign keys (Shopware documentation). The responsible service, UnusedMediaPurger, first searches for all media without such references, passes the candidates to an event in small batches and deletes whatever is left afterwards (Shopware documentation). It checks the associations that the data model attaches to the media entity: product images, manufacturer logos, category images, property images, logos of shipping and payment methods, backgrounds of blocks and sections, documents and mail attachments (Shopware source code).

Anything not stored as a foreign key is outside this path, and that applies to many of Shopware's own features too. The guide names Shopping Experiences as an example, which store their configuration as JSON with nested references to media (Shopware documentation). For such cases there is the UnusedMediaSearchEvent: a subscriber receives the list of candidates and can mark individual media as used. It was introduced together with the dry run and the grace period in Shopware 6.5.1.0 (Shopware changelog), and with it came three subscribers in the core: for Shopping Experiences, for custom fields and for the theme (Shopware changelog). In the source code of version 6.7.16.0 there are still exactly these three (Shopware source code).

A look at these subscribers shows how narrowly they are scoped. The Shopping Experiences subscriber searches the configuration of the image, image slider and image gallery elements as well as the overrides that products and categories store for their layout (Shopware source code). The custom fields subscriber covers fields of type media and selection fields that point to media. The theme subscriber reads the media fields of the theme configuration. Anything outside these patterns counts as unused: a custom Shopping Experiences element from an extension, a custom field of type text into which an import writes a media ID, or an image that only appears as an address in a description or a mail template (Shopware source code).

Where the image is usedDoes the cleanup recognize the usage?Basis
Product, manufacturer, category, propertyincluded Association in the data model
Logo of a shipping or payment method, document, mail attachmentincluded Association in the data model
Shopping Experience: image, image slider, image gallery elementincluded Shopping Experiences subscriber
Custom field of type media or media selectionincluded Custom fields subscriber
Media field in the theme configurationincluded Theme subscriber
Custom element or JSON configuration of an extensionOnly if the extension subscribes to the eventUnusedMediaSearchEvent
Image address in a description, text element or mail templatenot included No reference, just text
Newsletters, ads, partner sitesnot included Outside the database

Files embedded outside the shop have no technical protection at all. A newsletter that loads its images from the shop address fetches them there every time it is opened, even weeks after sending. The same applies to ads and partner sites that link a campaign image directly. If the cleanup deletes such a file, the newsletter archive shows a gap, and nobody in the shop notices. For individual checks, the Administration shows in the Used in area where in the shop a media item is used, with direct links to each place (Shopware documentation). That does not scale to a long list, but it works well for the handful of files someone is unsure about.

Extensions have to report their media

For extensions, the path is clearly documented. An extension that references media and cannot use foreign keys registers a subscriber for the UnusedMediaSearchEvent, fetches the candidates with getUnusedIds and returns the ones it needs with markAsUsed (Shopware documentation). The example in the guide is an image slider that stores its images as a list of IDs in a JSON column, which is exactly the design many extensions for banners, sliders and landing pages use. Whether an installed extension does this is written in its code, not in its store description. On a copy of the shop, the check takes only a few minutes:

Shop directory
# Which extensions report their media?
grep -rl --include='*.php' 'UnusedMediaSearchEvent' custom/plugins vendor \
  | grep -v '^vendor/shopware/'

Every extension that keeps media in its own structures and is missing from this list is a candidate for losses. Two more points belong in the check. Shopware does not load a deactivated extension, so its subscriber reports nothing; the images it expects when it is next activated count as unused until then. And apps, which do not run as a plugin inside the shop, cannot subscribe to a PHP event. For them, what matters is whether their media sit in custom fields of type media or in the standard Shopping Experiences elements. How to spot extensions that are no longer maintained and therefore will not close such gaps is covered in the article on abandoned plugins.

  • Sliders, banners and landing pages: extensions that store images in their own tables or JSON configurations must appear in the hit list.
  • Custom Shopping Experiences elements: in Shopping Experiences, the core subscriber only checks the image, image slider and image gallery elements (Shopware source code). A custom element with an image picker needs its own subscriber.
  • Imports and interfaces: if an import writes media IDs into a custom field of type text, the custom fields subscriber does not find them, because it only reads fields of type media and media selection (Shopware source code).
  • Deactivated extensions: before the deletion run, activate them, uninstall them cleanly or restrict the run with --folder-entity to folders where they have no media.

Before the first run in production

Checking the extensions belongs before the first deletion run, not after it. An image that an extension without a subscriber keeps in its configuration disappears from the shop when deleted, usually without an error message: the extension simply no longer finds the file.

The dry run and the report

For a first look, there is --dry-run: the command shows a paginated list of the files that would be deleted, without deleting anything (Shopware documentation). Each page shows file name, title, upload date and file size (Shopware source code). For handing the list to marketing or editorial staff, the second option works better: --report delivers the same columns as a machine-readable CSV list that can be redirected into a file (Shopware documentation). This option has existed since Shopware 6.5.4.0 (Shopware changelog). The two cannot be combined; the command then stops with a message (Shopware source code). Anyone still running a version older than 6.5.1.0 has neither the dry run nor the grace period and should run the command there on a copy only.

Shop directory
# Dry run: paginated list, deletes nothing
bin/console media:delete-unused --dry-run

# Report as a CSV file for approval
bin/console media:delete-unused --report > unused-media-report.csv

# only the default folder for product media and its subfolders
bin/console media:delete-unused --report --folder-entity=PRODUCT > unused-product-media.csv

The report lists no folders and no paths, just file name, title, date and size. For the review, that is usually enough: sorted by size, the files taking up the most space come first; sorted by date, the oldest ones. The same questions help with every row. Does the image belong to a sale that is over? Does the same image exist again under a different name? And will it be needed again next year, for example as a seasonal image for Christmas or the summer sale? Because the command has no exclusion list, the last group needs a usage or a snapshot of its own before the deletion run, more on that below. Before a row is approved, it pays to compare it with the shop's texts, because the cleanup does not see embedded addresses. The following query counts where a file name appears in descriptions, Shopping Experiences and mail templates:

Comparison with texts (read-only)
SET @f = '%herbst-kampagne%';
SELECT 'product' AS location, COUNT(*) AS hits FROM product_translation WHERE description LIKE @f
UNION ALL SELECT 'category', COUNT(*) FROM category_translation WHERE description LIKE @f
UNION ALL SELECT 'cms_slot', COUNT(*) FROM cms_slot_translation WHERE CAST(config AS CHAR) LIKE @f
UNION ALL SELECT 'mail_template', COUNT(*) FROM mail_template_translation WHERE content_html LIKE @f;

A hit does not necessarily mean the file is needed; it means a person has to take a look. The query only reads and can just as well run on a copy of the database. Anyone repeating the dry run on a staging environment anyway can rehearse the deletion run there too and then check the home page, categories, Shopping Experiences and mail templates for missing images.

First back up, then delete

The command deletes the media records along with their files (Shopware documentation), and there is no trash in between. From the run onward, only the backup helps, and the BSI describes what that means: if a file is deleted, intentionally or unintentionally, it is no longer available in any later backup; restoring it requires an older backup created before the deletion (BSI implementation notes CON.3). Whether that generation still exists depends on retention. For shops on our servers, we keep daily backups for 7 days, weekly ones for 4 weeks and monthly ones for 6 months (backup service description). A Christmas banner deleted on November 30 and needed again next November is then no longer contained in any snapshot.

The answer is not to keep every snapshot longer but to create a dedicated snapshot before the cleanup. For losses that are only noticed very late, the BSI recommends additional quarterly or annual backups (BSI implementation notes CON.3). A media cleanup is exactly such a case: whether an image is missing often only shows when next year's campaign starts. How to create a backup outside the rotation is covered in the article on the year-end backup. The same principle applies to media on a smaller scale: database and media files are backed up right before the run, labeled as a snapshot of their own and kept until the next season is over. A mirrored media storage does not replace this snapshot, because according to the BSI, deletions in a mirror take effect on the mirrored copy immediately (BSI implementation notes CON.3).

  1. Pull the report and store it as a dated CSV file, so it stays traceable what was deleted.
  2. Get approval: marketing and editorial staff mark what should stay. The command has no exclusion list (Shopware source code): whatever should stay gets a usage before the run; seasonal images without a usage then exist only in the dedicated snapshot.
  3. Back up the state: database and the directories public/media and public/thumbnail together, separate from the web server.
  4. Test the restore: bring back a single file from the snapshot before anyone relies on it.
  5. Delete in the maintenance window with the same options as in the report. Right before that, pull the report again and compare it with the approved list, because the run deletes whatever is unused at that moment (Shopware source code). Then spot-check the storefront and mail templates.

Record and file belong together

A restored file alone does not bring the image back into the media library. Shopware keeps every media item as a record with its thumbnails in the database, while the file lives in the filesystem. Anyone restoring individual images needs both from the same snapshot, or uploads the file again and reassigns it. How to plan a restore is covered in the article on backup strategies.

Grace period, folders and cron jobs

The grace period protects files that have just been uploaded. By default, media from the last 20 days stay untouched even if they are unused; --grace-period-days changes the value (Shopware documentation). It is measured against the upload date of the file (Shopware source code). For everyday work, the default is usually enough: anyone uploading images today and assigning them tomorrow loses nothing. It is different for material that sits in the shop long before it is used, such as images for the January sale that are uploaded in early December. For shops with long lead times, a grace period longer than their own campaign lead time makes sense, or the images are placed in advance in an image element of a Shopping Experience layout that is not yet assigned to any category or landing page.

With --folder-entity, the command limits the search to the default folder of one area; the documentation gives --folder-entity=PRODUCT for product images as an example (Shopware documentation). Subfolders of that default folder are included (Shopware source code). For the first run, this is a good starting point: product media are cleanly associated and rarely entangled with extensions. The Shopping Experiences folders and custom editorial folders only follow once the extension check is complete. Media outside the selected default folder are not covered by a run with this option.

That leaves the question of automation. When called interactively, the command asks before deleting whether it should really delete, and the suggested answer is no. Without interaction, meaning with --no-interaction or without an attached terminal as in a cron job, this prompt is skipped and the command deletes right away (Shopware source code). A nightly deletion run without approval therefore takes every file that happens to sit in the shop without an assignment on that day and is older than the grace period. Automation makes sense for the report: it runs weekly, writes the list to a file, and a person decides. How to notice silent failures of such background jobs is covered in the article on cron jobs and imports.

crontab
# Report every Monday at 6 a.m., deletion only after approval
0 6 * * 1 cd /var/www/shop && bin/console media:delete-unused --report --no-interaction > var/log/unused-media-report.csv 2>> var/log/unused-media-report.err

Thumbnails and orphaned files

Thumbnails belong to the media item and are deleted together with it (Shopware source code). Apart from that, thumbnail files without a record can appear, for example after changed sizes, aborted runs or server moves. Which sizes are generated is set in the thumbnail settings of each folder, and after a change the thumbnails can be regenerated with media:generate-thumbnails (Shopware documentation). The --strict option additionally checks whether the files for existing thumbnails are actually present on disk (Shopware source code).

For leftovers, there is media:delete-local-thumbnails. Without further options, the command is meant for shops that have thumbnails generated externally, and it then deletes all local thumbnails (Shopware documentation). In Shopware 6.7.16.0 it also knows --orphans: this option only deletes thumbnail files without a database record and keeps referenced thumbnails (Shopware source code). --force, on the other hand, deletes all thumbnail files, and the storefront shows no thumbnails until they are regenerated (Shopware source code). Older versions lack --orphans; there, cleaning up thumbnail leftovers belongs in the hands of someone who measures the state first.

Shop directory
# Measure disk usage before and after
du -sh public/media public/thumbnail

# remove only thumbnail files without a record (Shopware 6.7.16.0)
bin/console media:delete-local-thumbnails --orphans

# regenerate missing thumbnail files
bin/console media:generate-thumbnails --strict

Measuring before and after is part of every run. The size of the directories public/media and public/thumbnail, the number of rows in the report and the size of the next backup show what the cleanup achieved, and help to judge next time whether the effort pays off. How image formats and thumbnail sizes affect load time is covered in the article on image maintenance; we handle the ongoing care in performance maintenance.

On the side: the WordPress media library

Anyone running a WordPress site next to the shop finds a similar situation there, with less protection. The media library offers a filter for media that are not attached to any post or page (WordPress documentation). Attached only means that the file is assigned to a post or page, though; an image embedded in another post, a widget or the theme can still show up as unattached. Deleting based on this filter alone is therefore riskier than the Shopware cleanup, which at least checks references in the data model. On the storage side, there is one more factor: images whose width or height exceeds a threshold of 2560 pixels by default are scaled down by WordPress on upload, and the scaled version is used as the largest image size (WordPress Developer Resources). The original is kept and can be retrieved through a dedicated function (WordPress Developer Resources). For backup and disk usage, both files count. We take care of such sites in WordPress maintenance.

Media cleanup as a fixed cycle in the maintenance plan

Cleaning up once helps; a cycle helps for good. The time after Cyber Monday works in its favor because the sale images are fresh and the people involved still know which file belonged to what. The ongoing Christmas season argues against deleting right away: every change to the shop before Christmas should fit the change freeze in the maintenance plan. A practical approach separates the two. The report is created now, approval runs in December, and deletion happens in January, once the year-end backup of December 31 is in place. That backup then contains every file from the approved report and, as described in the article on the year-end backup, is kept outside the rotation.

  • After every major sale: pull the report, mark sale images, get approval.
  • Quarterly: report across all folders, comparison with texts and mail templates, measurement of public/media and public/thumbnail.
  • After every change to extensions: check new or updated extensions for the event before the next deletion run.
  • Before a theme change or relaunch: clean up images of the old layout only after the switch and after a backup.
  • Yearly: review seasonal images in the archive and decide what to keep.

The media cleanup fits with the other tasks after a sale, such as the inventory of price rules after Cyber Monday. Both can be prepared in the same maintenance window and completed together in January.

What we take on in maintenance

In Shopware maintenance, we add the media cleanup to the maintenance plan as a fixed item on request. This includes the report with an approval list, checking the installed extensions for the UnusedMediaSearchEvent, comparing the candidates with descriptions, Shopping Experiences and mail templates, a dedicated backup snapshot before deletion and the run in the agreed maintenance window with measurements before and after. For shops on our servers, the backup service stores the daily backup on storage separate from the web server at two locations in the EU (backup service description), and media and uploads are part of the backup scope. How maintenance, backups and response times within service hours work together is set out in the SLA maintenance contract.

Sources

This article is based on data from: Shopware documentation, pages Commands Reference, Prevent Deletion of Media Files Referenced in Plugins and Filesystem (developer.shopware.com) as well as Media (docs.shopware.com); Shopware changelog, entries Improve delete unused media command for version 6.5.1.0 and Generate unused media report for version 6.5.4.0 (GitHub); Shopware source code in version 6.7.16.0, classes DeleteNotUsedMediaCommand, UnusedMediaPurger and MediaDefinition, the three subscribers for Shopping Experiences, custom fields and theme, DeleteThumbnailsCommand, GenerateThumbnailsCommand and the migrations for thumbnail sizes (GitHub); BSI implementation notes for module CON.3 Data Backup Concept; WordPress documentation, page Media Library Screen; WordPress Developer Resources, pages big_image_size_threshold and wp_get_original_image_path; backup service description. All sources retrieved on November 11, 2026.

Related Articles

Performance & shop operations

Too many price rules: when the listing sorts incorrectly

For sorting and filters in the listing, Shopware evaluates up to 100 matching rules by default. Which ones count, how to check and what a higher limit costs.

18 min read
Security

Debug Mode in Production: Checking Profiler and Error Pages

After a hotfix, debug mode often stays on. What strangers see on error pages and in the profiler, and how to check Shopware and WordPress after each change.

19 min read
Monitoring & incident response

Shopware system check: what system:check reports and misses

Shopware ships its own system check, system:check. What its exit codes, contexts and HTTP endpoint report, what it cannot see and how to evaluate it safely.

19 min read