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 🙂
Features
- 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.
Remember to leave a comment if you used the script!
COMMANDS
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.
EXAMPLES
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 /m/1/m1.jpg /m/2/m2.jpg /m/3/m3.jpg /m/4/m4.jpg /m/5/m5.jpg 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/example.com/public_html
GLOBAL OPTIONS
-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
REQUIREMENTS
This scripts requires Ruby 2.0 or greater plus the following ruby gems:
- nokogiri ~ 1.6
- commander ~ 4.2
- mysql2 ~ 0.3
LICENSE
GNU General Public License, version 3 (GPLv3) – http://www.gnu.org/licenses/gpl-3.0.html
Waiting for your PHP version on Magento: Safely Clean Unused Images
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.
For Magento 2 hers is the extension http://shreejiinfosys.co.in/index.php/delete-unused-images-magento-reg-2.html