Image Cleaning for Magento 2

Image Cleaning for Magento 2
Magento 1Magento 2
Magento Edition
Pro installation
  • 48
  • 42
  • 5

Many web-stores with a long history run into a problem with the server's 'pub/media/' folder getting pumped and taking up plenty of precious storage space. Tens, even hundreds of gigabytes of data that take more and more despite the number of goods remains stable.

Having examined the contents of the media folder, we concluded it contains all ever uploaded images including those unusable by Magento. Real heaps of rubbish! And what surprised us even more was when we specially tested the process of removal of a product/category containing images as well as removal/editing of images under product's 'Images And Videos'. It appeared Magento only removes records about the images, not their physical files. This becomes one of the major causes for the website to occupy more space and to slow down a system call for files and images due to server's extreme file system load.

The extension we present allows you to thoroughly scan and safely clean the 'pub/media/' folder.


The procedure that the extension performs is not intended to be run only once, else the media folder will start growing again. This is why we specifically added a programmable cron job for the extension to scan the storage and automatically clean it once a week/month. To configure resources to clean and run a clean procedure manually, go to the extension's configuration page:
• Stores > Configuration > SAFEMAGE: Image Cleaning

1) Resources to clean are represented by the following options:
• Image files that do not belong to any product
• Image files that do not belong to any category
• DB record related to a product's nonexisting image file
• DB record related to a category's nonexisting image file

2) Based on capabilities of your server, specify the One-Time Limit for cleaning to make sure your system doesn't hang for a long time

3) Automatize the whole process by enabling and setting up a cron schedule

4) You can always make a manual clean run by hitting the 'Clean Now' button

The cleaning results are always logged in
• /var/log/cleaned_images.log

5) In case you have too many images to process and the admin panel method fails, you are free to use CLI to run a cleanup procedure:
php bin/magento image-cleaning:clean-images


  • Safely cleans dead files in 'pub/media/' which may greatly reduce the folder's size
  • Matches physical file against its record in the DB
  • Removes product's and category's obsolete and unutilized images
  • Removes media's DB records relating to nonexisting image files of products and categories
  • Configurable cron schedule for cleaning
  • Run a cleanup procedure via CLI



Magento Open Source (CE) 2.3.x, 2.4.x
Magento Commerce (EE) 2.3.x, 2.4.x


We provide comprehensive product support for all the Magento extensions available on this site starting from the moment of your purchasing them. If you need help on the extensions installation or on any issues relating to our products, our dedicated team of proven technical experts is at your service, ready to assist you at any time.


new feature bugfix
v.2.1.9 (Jul 26, 2023)
Minor code refactoring.
v.2.1.8 (Aug 28, 2022)
Minor code refactoring.
v.2.1.7 (Dec 20, 2021)
Code refactoring.
v.2.1.6 (Nov 30, 2021)
Code refactoring.
v.2.1.5 (Oct 7, 2021)
Minor code changes.
v.2.1.4 (Dec 30, 2020)
Added support for symlinks in the media folder.
v.2.1.3 (Dec 4, 2020)
Added support for Magento Commerce 2.4.x.
v.2.1.2 (Nov 3, 2020)
Addressed an issue with image cleanup for Magento 2.4.
v.2.1.1 (Oct 16, 2020)
Addressed an issue with category cleanup.
v.2.1.0 (Sep 24, 2020)
Added the ability to run a cleanup via CLI.
v.2.0.8 (Sep 7, 2020)
Added support for Magento 2.4.0.
v.2.0.7 (Jul 21, 2020)
Addressed an issue with placeholders and watermarks.
v.2.0.6 (Dec 20, 2019)
Added support for Magento 2.3.3.
v.2.0.5 (Aug 16, 2019)
Added ability to ignore files from the 'cache' folder.
v.2.0.4 (Jan 18, 2019)
Added support for Magento 2.3.
v.2.0.3 (Sep 13, 2018)
Added support for Magento 2.1.
v.2.0.2 (Jun 4, 2018)
Added better support for Magento 2.2.3.
v.2.0.1 (Mar 7, 2018)
Addressed a ‘chdir: no such directory’ warning.
v.2.0.0 (Feb 12, 2018)
Initial release.


    I managed to delete around 100 gigabytes of files. Awesome!
    by Milan January 31, 2022
Write Your Own Review

Only registered users can write reviews. Please, log in or register