×
Sign In

BLOG

Magento Media Image Cleanup

This Post is about cleaning out extra media images that are created by Magento’s functionality.

For better understanding, consider a case where we upload CSV of 1000 products, each containing one main image and also one thumb image. So, there would 2000 images uploaded in media folder at location ‘media/catalog/product/…’

Imagine if you set up a cron that uploads products from some CSV file everyday at midnight. It’s not the case that you are going to add/update/delete products everyday. So, if you have not updated anything for one week in CSV file, which is going to be run everyday using cron and products are going to be imported everyday for that whole week. But as the product_id would be same for the products that are not changed in any means, duplicate product would not be created. But the images would be duplicated and copied to ‘media/catalog/product/…’

Consider a case, for a product with images abc.jpg and abc_thumb.jpg. Now, when the same product is uploaded again, then abc_1.jpg and abc_thumb_1.jpg are created in ‘media/catalog/product/…’. Likewise after the whole week, images would be created as abc_2.jpg and abc_thumb_2.jpg and it continues until the last day of the week.

So, after 1 week, 2000 images would be now 14000 images. Just imagine what it would after 1 month, if old products are still there in CSV file that is uploaded by cron everyday. Ultimately, the size of media folder would increase day by day and server memory for our site would also increase. This may cause many serious problems.So, to avoid such problems, extra images from ‘media/catalog/product/…’ should be deleted of those images, whose entries are not found in db.

Now, there is one pair of image for each product that is uploaded repetitively.

Further flushing out the images of cache folder that is created by Magento at ‘media/catalog/product/cache’,

Above code would delete the ‘cache’ folder from ‘media/catalog/product/’. So, now, extra catalog images that are created due to Magento cache would be deleted every time.

While uploading process of images using .csv file, Poeple usually create some temporary folder. So, after the images are copied to Magento directory structure, that temporary folder is not needed. To save memory space on server, it is a good practice to delete, all the extra folders, images, etc. So, to delete that temporary folder.

For example temporary folder named ‘import’ in ‘media’ folder of Magento and it contains .csv file and other images that must be deleted at the end.

It can be done through adding following code:

 

Issue Fixed By: Nimit Shah
Guided By: Sunil Patel


Contact Form
Footer Content