Remove unused Magento product images

Magento has the nasty habit of leaving unused product image files in it’s var/media directory. If you, like one of our clients, import your catalog at a regular interval using Magento’s Dataflow or similar this will fill up your precious data space. Especially when your catalogue consists of 12.000+ SKUs with one or more product images like was the situation here…

The solution
There is a solution in the form of the Image Clean extension created by Defcon. That extension however cannot be scheduled using cron, and it requires manual interaction. Panticz was probably thinking the same thing, and put together a shell script to clean up the unused Magento product images. However, his script needs a config file containing some variables, also the paths probably need manual adjusting if you want to use it yourself.

A man of little words
Panticz, apparently being a man of little words, didn’t leave an instruction to go with it. So we put together a version of his script that is easier to deploy. It will configure itself if you tell it where your Magento install is located. Just put it somewhere where you can run it, and make sure you change MAGENTO_PATH=”/path/to/your/magento/httpdocs” to the path of your Magento install. It’s suited for use in a cronjob, say once a week.

DB_USER=$(sed -n 's|<username><\!\[CDATA\[\(.*\)\]\]></username>|\1|p' ${MAGENTO_PATH}/app/etc/local.xml | tr -d ' ')
DB_PASS=$(sed -n 's|<password><\!\[CDATA\[\(.*\)\]\]></password>|\1|p' ${MAGENTO_PATH}/app/etc/local.xml | tr -d ' ')
DB_NAME=$(sed -n 's|<dbname><\!\[CDATA\[\(.*\)\]\]></dbname>|\1|p' ${MAGENTO_PATH}/app/etc/local.xml | tr -d ' ')
DB_PREFIX=$(sed -n 's|<table_prefix><\!\[CDATA\[\(.*\)\]\]></table_prefix>|\1|p' ${MAGENTO_PATH}/app/etc/local.xml | tr -d ' ')

function search_db() {
	COUNT=$(mysql -u ${DB_USER} -p${DB_PASS} ${DB_NAME} --execute="SELECT count(*) FROM ${DB_PREFIX}catalog_product_entity_media_gallery WHERE value = \"$1\"")
	echo $(echo ${COUNT} | cut -d" " -f2)

echo "Starting image cleanup " $(date) | tee -a ${LOG}
for IMG in $(find ${IMG_PATH} -name '*.jpg' ! -path '*cache*' ! -name 'google*'); do
	if [ $(search_db ${REL_IMG/'${MAGENTO_PATH}/media/catalog/product'/}) != 1 ]; then
		for CACHE_IMG in $(find ${MAGENTO_PATH}/media/catalog/product/ -name "${IMG}"); do
			echo "Found unused image ${CACHE_IMG}"
			if [ "$1" ] && [ $1 == 'cleanup' ]; then
				echo "Removing unused image ${CACHE_IMG}" | tee -a ${LOG}
				rm "${CACHE_IMG}"
		echo "Not touching " ${IMG}
echo "Finished image cleanup " $(date) | tee -a ${LOG}

How can I use this?
1. Download the script here. Extract it and put it somewhere in your Magento installation. For example in the shell directory.
2. Edit the script and change the MAGENTO_PATH to the absolute path (starting at /, but not ending with a /) of your Magento installation. If you do not know where that is, leave a comment, or bang your head against a wall, whichever suits you best/
3. Make the file executable. You can do this using your (S)FTP software or on the command line. For example using “chmod +rx”. Again leave if a comment if your having trouble doing that.
4. Run it in test modus (without parameters). It won’t delete anyhing! But if you see does not want to touch some files, but remove others you know its working. If you are happy go to the next step.
4. Run it on the shell with the paramter cleanup. So ‘./ cleanup’. Or create a cronjob in your hosting panel calling this script once a week.

If you lose images you still needed you’re on your own, so test things out (as described) somewhere and always make sure you have a backup. As with all our free code, we are not responsible for the havoc it may cause. It’s tested for Magento 1.4+ in several real life environments. We however are not obligated by any way to provide support if thing go wrong (we however, always do, if you ask us nice… or if you buy us candy), nor are we responsible for anything, in any way ;-).

Like this free extension?
Let us know, and like our Facebook page! Want more? Leave a comment expressing your wishes in regard to this extension, and we will keep you in mind working on updates.