13

I have more than 50,000 images in pub/media of products.

But there are so many unused images so I want to remove all images which are not associated with any product.

For Magento 1.9.x this can be a Reference link but I want the same PHP script for Magento 2.

3 Answers 3

17

You can use the Hackathon EAV cleaner module for this. After installation you can use the command eav:media:remove-unused

You can find it on Github: https://github.com/magento-hackathon/EAVCleaner/tree/magento2

7
  • let me try it, will let you know. Commented Dec 19, 2016 at 11:53
  • It's working for me, Thank you so much :) Commented Dec 19, 2016 at 12:05
  • By the way, i've forked the repo so it is now possible to use composer installation: github.com/Vendic/EAVCleaner. The old repo seems to be inactive.
    – Tjitse
    Commented Mar 1, 2019 at 12:06
  • I've tried to install this. Composer doesn't find it and copying it directly doesn't work for me. Can you point me to or provide some simple instructions please?
    – PedroKTFC
    Commented May 29, 2019 at 21:59
  • Try composer require hackathon/magento2-eavcleaner
    – Tjitse
    Commented May 30, 2019 at 7:53
3

The easier method that I found is installing the module below and running these commands.

composer require ekouk/imagecleaner "dev-master"
bin/magento setup:upgrade
bin/magento setup:di:compile
bin/magento cache:flush

Run in check only mode which will report the number and size in Mb of unused images.

bin/magento ekouk:cleanimages

Then run this command to delete the files.

bin/magento ekouk:cleanimages -d

This module will only remove jpg, png, jpeg images from pub/media/catalog/product which are not referenced in the database. The default function without any switches will just report on unused files and NOT delete anything You must use the -d switch to remove files.

This module is provided free of charge with no warranty.

Please ensure you have a backup copy of your pub/media/catalog directory just in case anything does go wrong.

I found it and started to use it from here.

1
  • FYI, before "composer require" can succeed, you also need to add the custom repository into your composer.json file. See the instructions linked at the bottom of this solution.
    – jdhildeb
    Commented Sep 6, 2022 at 18:57
1

As an alternative there is this tool from Thomas Negeli that I used successfully to remove over 100 gb of unused images in Magento 2.3.6: https://github.com/ThomasNegeli/M2CliTools

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.