Automation, Magento

Automate Magento Image Cache – Don’t let your website down when catalog image cache is flushed!

Clear Catalog Image Cache

No Data Received error after clicking on  Flush Catalog Image Cache

Problem – How to remove Magento Image Cache selectively, when you need to replace existing images and Clicking “Flush Catalog Image Cache” on Magento Cache Management will empty the cache folder which consumes lot of cpu plus memory, and for replacing few images Magento will generate all product images (say for replacing 2 product images it will remove all your 3000 product catalog image cache).

Goal – To Develop an automated solution for removing catalog image cache selectively.

What actually removing Magento catalog image cache does is. it empties the media/catalog/product/cache folder.

Directory where it executes the operation : <Magento Root Directory>/media/catalog/product/cache

Let’s first install a program called ‘incron‘, which is used to monitor modified directories and files and response to the File Events.

– Red Hat / Centos : sudo yum install incron

– Ubuntu / Debian based flavours : sudo apt-get install incron

Turn Service on :

# service incrond start

You also need a script to search / remove the SKU images replaced:

let’s assume you are in /root directory,

# vim cachefilesmonitor.sh

Add Following content on it:

find /<Your Magento Directory>/media/catalog/product/cache/ -name “$1*” -exec rm -f {} \;

save and close it.

Now type the following command to edit your incrontab:

# incrontab -e

<Your Magento Root Directory>/media/catalog/product/cache/<Directory for your Catalog Image (SKU)> IN_CLOSE_WRITE /root/cachefilesmonitor.sh $#

Example(For SKU’s like KP00234.KP00235):

/var/www/html/media/catalog/product/cache/K/P IN_CLOSE_WRITE /root/cachefilesmonitor.sh $#

Similarly, for multiple SKU ranges you need to add incrontab rules.
You can check the logs using :

tail -f /var/log/syslog (Centos)

tail -f /var/log/messages (Ubuntu)

Now, you don’t need to worry about cleaning your Catalog Image Cache manually.

Ciao!

You Might Also Like