Delete unused product images in magento

Delete unused product images in magento

July 8, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Delete unused product images in magento

Automating Image Cleanup in Magento: A Developer’s Guide

As an e-commerce platform, Magento often accumulates numerous unused product images over time that clog up your server space. While the image-clean module provides manual control over this issue, many store owners prefer automated solutions to save time and ensure consistent housekeeping.

Identifying Unused Images in Magento

Before deletion, we need to find all the unused images residing under /media/catalog/product/. These are files that have no corresponding product or category association. The image-clean module already performs this detection through its database queries and file listings.

To achieve similar results programmatically, you can use PHP’s built-in functions like scandir() to recursively list all files in the directory:

$images = scandir('/media/catalog/product/');

However, this alone won’t distinguish between used and unused images. You’ll need to cross-reference these image names against your product and category tables (catalog_product_entity and catalog_category_entity) to filter out only the unused ones.

Deleting Unused Images Automatically

Once you’ve identified all unused images, it’s a simple matter of deleting them using PHP’s unlink() function:

foreach ($unusedImages as $image) {
    unlink('/media/catalog/product/' . $image);
}

Implementing Cron Jobs for Scheduled Cleanup

To automate this process and run it daily, you’ll want to create a cron job. A cron job is essentially a scheduled task that runs at specified intervals.

Here’s how you would set up the cron job:

Step 1: Create an Image Cleanup Script

Create a new PHP script (e.g., clean_unused_images.php) in your Magento root directory and place the image cleaning code inside it. Make sure to include any necessary Magento bootstrap logic at the top.

Step 2: Configure Cron Job

To add a new cron job, you’ll need to modify your server’s crontab file (usually located at /etc/crontab on Linux systems). Open this file as a superuser with a text editor like nano:

sudo nano /etc/crontab

Step 3: Define the Cron Job Schedule

Add a new line in the format below, specifying your desired schedule:

0 2 * * * php /path/to/clean_unused_images.php >> /path/to/cron_log.log 2>&1

In this example:

  • 0 2 means run at 2:00 AM every day.
  • The path should point to the script you created in Step 1.

Step 4: Save and Close

Save the file and exit. Your cron job is now set up and will run automatically according to your schedule, deleting any unused product images it finds each time.

Conclusion

Automating image cleanup in Magento provides significant benefits for both server efficiency and general housekeeping. By following this guide, you can implement a robust solution that requires minimal manual intervention.

Remember, always backup your data before performing mass deletions of any kind. Also, ensure you have sufficient permissions to delete files from the /media/catalog/product/ directory.

For more advanced Magento customizations and performance optimizations, I recommend checking out our comprehensive guides at LaravelCompany.

This automated script provides a straightforward, sustainable way to keep your Magento site tidy and efficient. Happy coding!

Catalog Database Extensions Cron

Our network