How to Clean Up Unused Product Images in Magento 2

Where orphaned product and category images come from in Magento 2, how to find them safely, and what to check before you delete anything.

How to Clean Up Unused Product Images in Magento 2

Every time a product is deleted, an image is replaced, or a catalog is re-imported, Magento 2 can leave the old image files behind in pub/media/catalog/product. Nothing references them any more, but they stay on disk, take up space, and make backups and deployments slower. This guide explains where the leftovers come from, how to find them safely, and what to check before you delete anything.

Why unused images pile up

Magento stores product images as files under pub/media/catalog/product and records which file belongs to which product in its media gallery tables. Deleting a product or removing an image from the gallery removes the record, but not always the file. Common sources of orphaned files are:

  • products deleted from the admin or by an import,
  • images replaced with a new version,
  • bulk imports that were run more than once, or rolled back,
  • category images that were changed or removed.

Over years of catalog changes, a store can collect a large number of these files.

What not to delete

Not every file Magento doesn't list in the gallery is safe to remove. Leave these alone:

  • pub/media/catalog/product/cache: resized copies Magento creates for the storefront. Clear them with Flush Catalog Images Cache in System > Cache Management (and regenerate them with php bin/magento catalog:images:resize), not by hand.
  • Watermark and placeholder folders: used by your theme configuration.
  • Images used by other extensions: some modules store their own image fields, such as a separate category thumbnail. A scan that only checks Magento's native fields will see those as unused.

How to find the unused images

The reliable method is to compare the files on disk with the images Magento actually uses:

  1. List every file under pub/media/catalog/product, skipping the cache, watermark and placeholder folders.
  2. List every image in the product media gallery (catalog_product_entity_media_gallery and catalog_product_entity_media_gallery_value), not just the base images.
  3. Any file in the first list that is not in the second is unused.
  4. Do the same for pub/media/catalog/category against each category's image.

You can script this yourself, or use our Image Cleaner for Magento 2, which does the comparison from the admin: click Load Unused Images and review the list, with a thumbnail and path for each file.

Delete safely

Deleting a file from disk is permanent. Before any cleanup:

  1. Back up pub/media, or at least the catalog folders.
  2. Review the list: look at the thumbnails and paths, especially for folders used by other extensions.
  3. Delete in batches, then check a few product and category pages on the storefront.
  4. Keep the backup until you are sure nothing is missing.

Image Cleaner never deletes anything by itself: you select the files to remove, one at a time or with a mass action. The user guide covers each step and the known limitations, such as remote (S3) media storage not being scanned.

How often to clean up

There is no fixed rule. A sensible time is after a large import, a catalog restructure, or a product range change, and before a migration, when a smaller media folder makes the move faster.

Still on Magento 1?

The same problem exists on Magento 1.9, in media/catalog/product. Image Cleaner for Magento 1 lists unused product and category images, exports the list to CSV, and lets you delete them from the admin.

If your media folder is part of a bigger performance or hosting problem, tell us about your store and we will look at the whole picture.

0 Comments

No comments yet. Be the first to comment.

Leave a Comment

Your email is never published.

Keep reading.

More guides from the Magexweb blog.

All articles

Rather we handle it?

Upgrades, security patches and platform moves are what we do every day. Tell us what your site needs, and we will reply with questions and next steps.

  • Email info@magexweb.com
  • Phone +91 99092 71288
  • Hours Mon–Fri, 10:00 AM to 8:00 PM IST
  • Office D-1002, Jasmin Green 1, Near Vaishnodevi Circle, Ahmedabad 382421, India

  • Free consultation
  • NDA available
  • Reply by email