After looking for a while at the top Google results for “magento clean images” and testing them a bit I realized most of the solutions barely work. And if they do, they tend to delete more images than you really want. After running one of the top scripts in the search results for example, most of the images on the site were completely gone.

So I wrote a small script to clean the Magento media folders myself. Decided to make in on Ruby (my scripting language of choice at this time). Sorry PHP / Bash, maybe next time 🙂


  • Clean unused images from the product catalog.
  • Clean the product catalog image cache.
  • Ready to use: automatically reads settings from app/etc/local.xml
  • FAST: I used it to safely clean about 45,000 images in just a couple of minutes.

View the Script

Remember to leave a comment if you used the script!


cache                Cleans the Magento product images cache
products             Cleans images that are not referenced anymore by the Magento catalog
help                 Display global or [command] help documentation

The script assumes its located in the magento_webroot/shell folder. If located elsewhere, the --webroot option must be specified to the location of the Magento webroot. Run clean.rb help [command] for more information.


To clean images that are no longer being used in the product catalog, run the following command:

shell/clean.rb products

Sample output:

Searching for files...
> Found 12952 files.
Querying for images...
Connecting to database...
Reading settings from app/etc/local.xml ...
> Found 12959 images.
Finding files to clean up...
Progress |==========.........| 56% complete
## REPORT ##
- Orphaned: 1234 files
- Missing: 7 files
Would you like to see a list of missing images (Y/n)? y
All done!

You can also schedule cleaning as cron tasks. Simply append the -f option to force (accept all prompts) and optionally also -s to silence the program (display no output):

shell/clean.rb products -f -s

And as mentioned above, if you’re not running the script on the webroot/shell folder of your Magento installation, then all you need to do is specify the path to the webroot as follows:

~/clean.rb products --webroot=/var/www/


-f, --force
    Perform irreversible actions without asking for consent. WARNING: use with caution!

-s, --silent
    Do not output information. Must be combined with --force to truly disable all output.

-h, --help
    Display help documentation

-v, --version
    Display version information

-t, --trace
    Display backtrace when an error occurs


This scripts requires Ruby 2.0 or greater plus the following ruby gems:

  • nokogiri ~ 1.6
  • commander ~ 4.2
  • mysql2 ~ 0.3


GNU General Public License, version 3 (GPLv3) –

3 Comments Magento: Safely Clean Unused Images

    1. Gabriel Somoza

      This was quite a long time ago, but I believe 1.9 was already out. So it will probably work on 1.9.0, but keep in mind that you’d be using the code at your own risk.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.