PHPackages                             baldwin/magento2-module-image-cleanup - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [File &amp; Storage](/categories/file-storage)
4. /
5. baldwin/magento2-module-image-cleanup

ActiveMagento2-module[File &amp; Storage](/categories/file-storage)

baldwin/magento2-module-image-cleanup
=====================================

Magento 2 module which can cleanup old image files that are no longer being used

v1.2.6(1y ago)8190.4k↓12.5%8[2 issues](https://github.com/baldwin-agency/magento2-module-image-cleanup/issues)MITPHPPHP ~7.3.0 || ~7.4.0 || ~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0

Since Sep 6Pushed 2mo ago10 watchersCompare

[ Source](https://github.com/baldwin-agency/magento2-module-image-cleanup)[ Packagist](https://packagist.org/packages/baldwin/magento2-module-image-cleanup)[ RSS](/packages/baldwin-magento2-module-image-cleanup/feed)WikiDiscussions v1-develop Synced 1mo ago

READMEChangelog (10)Dependencies (8)Versions (15)Used By (0)

Image Cleanup module for Magento 2
==================================

[](#image-cleanup-module-for-magento-2)

Purpose
-------

[](#purpose)

When adding products in your webshop, eventually you'll also have to delete some of those products. But sometimes Magento doesn't remove images associated with the products that you delete.
So you'll have to manually delete them from time to time from disk, which is hard to do manually. This module gives you some options to delete those lingering unused images from the disk so you can recover some diskspace on your server.

Implemented Features
--------------------

[](#implemented-features)

- It can find product image files on disk that are not referenced in the database and remove them
- It can do the same for the resized (cached) versions of those images
- It tries to not delete dynamically generated images files (like `webp` of `avif` files) if the original file is still being used, see [configuration](#configuration)
- It can detect entire unused resized (cached) directories that are no longer valid and remove them with all the files in there, see [below](#documentation-about-resizedcached-directories)
- It can detect and remove obsolete values in the `catalog_product_entity_media_gallery` database table
- It can find resized product image files that are corrupt and remove them

Watch out
---------

[](#watch-out)

- The module will always first output what it will delete, make sure you check the entire list before confirming, so that you aren't removing files you don't want to remove. Do **not** *test* this module on a production environment first before you fully understand what it will do!
- This module hasn't been tested when your Magento shop is configured to store image files in the database, your mileage may vary when you use that way of working. Feel free to open issues in case any occur, and we'll see if we can fix something...

Compatibility
-------------

[](#compatibility)

- This module should work with Magento 2.3.4 or higher
- The module should be compatible with PHP 7.3, 7.4, 8.1, 8.2, 8.3 and 8.4

Installation
------------

[](#installation)

You can use composer to install this module:

```
composer require baldwin/magento2-module-image-cleanup
```

Or download the code and put all the files in the directory `app/code/Baldwin/ImageCleanup`

After which you can then activate it in Magento using:

```
bin/magento setup:upgrade
```

Usage
-----

[](#usage)

There are 4 command line commands you can use execute:

- `bin/magento catalog:images:remove-obsolete-db-entries`
- `bin/magento catalog:images:remove-unused-hash-directories`
- `bin/magento catalog:images:remove-unused-files`
- `bin/magento catalog:images:remove-corrupt-resized-files`

There are some extra options for some of these commands:

```
      --no-stats        Skip calculating and outputting stats (filesizes, number of files, ...), this can speed up the command in case it runs slowly.
  -n, --no-interaction  Do not ask any interactive question

```

The `-n` option can be used if you want to setup a cronjob to regularly call these cleanup commands, it will not ask for confirmation before removing files, and will just assume you said 'yes, go ahead' (which can be dangerous!)

The module will output all the things it deleted in a log file `{magento-project}/var/log/baldwin-imagecleanup.log` so you can inspect it later in case you want to figure out why something got removed.

For optimal &amp; fastest cleanup, it's advised to run the commands in this order:

1. `bin/magento catalog:images:remove-obsolete-db-entries`
2. `bin/magento catalog:images:remove-unused-hash-directories`
3. `bin/magento catalog:images:remove-unused-files`
4. `bin/magento catalog:images:remove-corrupt-resized-files`

If you don't run these in this order, it might mean you'll need to run some of them a second time for them to find more things to cleanup or it might mean that they'll take longer then needed.

Configuration
-------------

[](#configuration)

There is a configuration section in the backoffice under: Stores &gt; Configuration &gt; Catalog &gt; Catalog &gt; Product Image Cleanup Settings

- **List of dynamically generated image file extensions**: Some Magento shops might have modules installed to dynamically generate `webp` or `avif` image files out of the original product image files. These files are usually not referenced in the database of Magento so by specifying those file extensions in the configuration, we can prevent them from being deleted accidentally. The module will still be able to remove those type of files when the original file is no longer referenced in the database.
    This feature only works properly when the dynamically generated image files use the same filename as the original file, so they can only be different in the file extension being used (either replaced or appended).

Documentation about resized/cached directories
----------------------------------------------

[](#documentation-about-resizedcached-directories)

Magento saves resized product images in certain directories in `pub/media/catalog/product/cache`The directory names are basically an md5 hash of a bunch of parameters like: width, height, background-color, quality, rotation, ... (which tend to be defined in the `etc/view.xml` file of themes) Sometimes, Magento tweaks how the hash gets calculated in certain newer versions of Magento, or your theme changes some parameter which both can make those hashes no longer being used.

This module has the option to detect such directories and can remove them together with all the files in there.

Note to self
------------

[](#note-to-self)

In our class `Baldwin\ImageCleanup\Finder\UnusedCacheHashDirectoriesFinder`, we borrowed some code from core Magento that was private and not easily callable. We made only very slight changes to deal with coding standards and static analysis, but it's mostly the same as the original source. These pieces of code were based on code that didn't really change since Magento 2.3.4.

It's important that we check with every single new Magento version that gets released, that the code in `Magento\MediaStorage\Service\ImageResize` doesn't change in such a way that we need to adapt our own implementation.

So this is something that needs to be double checked with every new Magento release.

###  Health Score

55

—

FairBetter than 98% of packages

Maintenance67

Regular maintenance activity

Popularity46

Moderate usage in the ecosystem

Community16

Small or concentrated contributor base

Maturity72

Established project with proven stability

 Bus Factor1

Top contributor holds 91.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~86 days

Recently: every ~96 days

Total

12

Last Release

402d ago

PHP version history (4 changes)v1.0.0PHP ~7.3.0 || ~7.4.0 || ~8.1.0

v1.0.2PHP ~7.3.0 || ~7.4.0 || ~8.1.0 || ~8.2.0

v1.2.3PHP ~7.3.0 || ~7.4.0 || ~8.1.0 || ~8.2.0 || ~8.3.0

v1.2.5PHP ~7.3.0 || ~7.4.0 || ~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/1d62cd17eb7811ff24ce4c4356f63182f29a910c80294eeef17d866a29bdea35?d=identicon)[baldwin](/maintainers/baldwin)

---

Top Contributors

[![hostep](https://avatars.githubusercontent.com/u/85479?v=4)](https://github.com/hostep "hostep (86 commits)")[![baldwinagency-pieter](https://avatars.githubusercontent.com/u/4758823?v=4)](https://github.com/baldwinagency-pieter "baldwinagency-pieter (8 commits)")

---

Tags

magento2magento2-extensionmagento2-module

### Embed Badge

![Health badge](/badges/baldwin-magento2-module-image-cleanup/health.svg)

```
[![Health](https://phpackages.com/badges/baldwin-magento2-module-image-cleanup/health.svg)](https://phpackages.com/packages/baldwin-magento2-module-image-cleanup)
```

###  Alternatives

[smile/elasticsuite

Magento 2 merchandising and search engine built on ElasticSearch

8044.5M33](/packages/smile-elasticsuite)[dotdigital/dotdigital-magento2-extension

Dotdigital for Magento 2

50374.2k18](/packages/dotdigital-dotdigital-magento2-extension)[mollie/magento2

Mollie Payment Module for Magento 2

1121.6M10](/packages/mollie-magento2)[swissup/module-search-mysql-legacy

Legacy mysql search for magento 2.4

10483.0k](/packages/swissup-module-search-mysql-legacy)[lizardmedia/module-varnish-warmer

Varnish Cache Warmer Magento2 module by Lizard Media

6276.8k](/packages/lizardmedia-module-varnish-warmer)[run_as_root/ext-magento2-google-shopping-feed

This module provides a google shopping product feed csv export.

438.7k](/packages/run-as-root-ext-magento2-google-shopping-feed)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
