Syns Posted September 25, 2015 Share Posted September 25, 2015 hello. is there any way to remove unused images from Prestashop? maybe a module? I have around 30k images but only less than 10k used. thank's Link to comment Share on other sites More sharing options...
navid68 Posted September 26, 2015 Share Posted September 26, 2015 I have the same need, the server space is running out and i would like to delete unused images. There is a script here but i miss the skill to use it, can anyone help? Thanks navid Link to comment Share on other sites More sharing options...
Syns Posted September 28, 2015 Author Share Posted September 28, 2015 Thank's for the link, I've tried it. just create a php file on your root, then copy the code on to it (I'm using the last one from mkbond777) and change the $shop_root url and $limit on the code. and then call it directly from your browser "yoursite.com/yourfile.php". the code is working, but I still don't understand what and how it does actually?! Link to comment Share on other sites More sharing options...
navid68 Posted September 28, 2015 Share Posted September 28, 2015 Can you confirm that it is working?? I am not a coder, i inserted my config but get a Parse error: syntax error, unexpected ':' on line 9 Would you help me to fix it? Can I send you a private message with my php file to look at? Thanks Navid Link to comment Share on other sites More sharing options...
Syns Posted September 28, 2015 Author Share Posted September 28, 2015 it says: count images database: 14833 ... result: files: 22954 checked: 922 not_found: 240 found: 682 sure just send me the php. Link to comment Share on other sites More sharing options...
navid68 Posted September 28, 2015 Share Posted September 28, 2015 Thanks Syns...message sent Navid Link to comment Share on other sites More sharing options...
navid68 Posted September 28, 2015 Share Posted September 28, 2015 Hi Syns, in the php script, i used 'mysql:host='95.142.159.11' and get the following error: Parse error: syntax error, unexpected '95.142' (T_DNUMBER) in what i am doing wrong Navid Link to comment Share on other sites More sharing options...
Syns Posted September 28, 2015 Author Share Posted September 28, 2015 could you show me the whole php code? Link to comment Share on other sites More sharing options...
hakeryk2 Posted February 28, 2019 Share Posted February 28, 2019 (edited) Ok, that is it! I was so angry that I can't find any working solution for this so I created my own and it is working. This is the code which You can place in Your file or just download the attached file and put it into main folder of Your shop and run http://yourshop.pl/remove_unused_images.php in Your browser. Things to consider if you are courious but not mandatory: Change $limit value from 19 line when testing, for example like 100, for production mode use $limit = $counted_images; if scan return 0 or there is an error just check if $scan_dir is ok by adding ddd($scan_dir); arround 11 line of code. Upload script again, test it if scan directory is properly created, if not then change $shop_root and $image_folder values from the beginning of the script, remove ddd part, reupload and run script again. It might take a long time so if You will run out of time then just run the script again. Feel free to not ask about anything because I won't support this code, I am not responsible for it and I won't help You. Likes for this post will be appreciated because there are some paid solutions, modules that are doing basically the same thing. This one is free. This will take some time to process - if You have large ammount of files it can exceed your max_execution_time limit on Your hosting. If this will happen You can try to take parts like 5000 or 10 000 and edit for ($i=1 to start with for ($i=5000 and then restart the script again and again with changing the limit <?php ####PUT THIS FILE INTO YOUR MAIN SHOP FOLDER#### // root path of the shop, almost no one needs to change something here. $shop_root = $_SERVER['DOCUMENT_ROOT']."/"; // need to have slash / at the end $image_folder = 'img/p/'; // also needs slash at the ennd $scan_dir = $shop_root.$image_folder; include_once($shop_root.'config/config.inc.php'); include $shop_root . 'config/settings.inc.php'; #---------------------------------------------# $last_id = (int)Db::getInstance()->getValue(' SELECT id_image FROM '._DB_PREFIX_.'image ORDER BY id_image DESC '); $counted_images = Db::getInstance()->executeS(' SELECT count(*) as qnt FROM '._DB_PREFIX_.'image '); $counted_images = (int)$counted_images[0]['qnt']; echo 'There was '.$last_id.' images in database but only '.$counted_images.' is used right now. Lets check how many of them are eating up our storage without no reason.<br>'; //$limit = 150; // for testing $limit = $last_id; // for production $removed_images = 0; for ($i=1; $i <= $limit; $i++) { if (!imageExistsInDB($i)){ $imageDir = str_split($i); $imageDir = implode('/', $imageDir); $path = $scan_dir.$imageDir; deleteImagesFromPath($path); } } function deleteImagesFromPath($path) { global $removed_images; $images = glob($path . '/*.{jpg,png,gif,jpeg}', GLOB_BRACE); if ($images){ foreach ($images as $file) { if (is_file($file)) { unlink($file); } } $removed_images++; echo 'Deleted images from folder ' . $path . '/' ."<br/>"; } } function imageExistsInDB($id_image){ return Db::getInstance()->getValue(' SELECT id_image FROM '._DB_PREFIX_.'image WHERE id_image = '.(int)$id_image ); } echo '--------------------------------------<br>'; if ($removed_images > 0) echo 'Hurray! We removed '.$removed_images.' product images!'; else echo 'Everything is ok with Your images. I did not removed any of them or I made it before. Good Job Presta!'; remove_unused_images.php Edited December 3, 2021 by hakeryk2 (see edit history) 4 5 1 Link to comment Share on other sites More sharing options...
ELEGANTAL Posted February 28, 2019 Share Posted February 28, 2019 This fails if you want to clear huge number of unused images. There is a module that does it well, check it out: Redundant Image Cleaner Link to comment Share on other sites More sharing options...
hakeryk2 Posted February 28, 2019 Share Posted February 28, 2019 (edited) It didn't failed when I was removing from database when 60 000 images were found. It will fail only if someone have some limits on their server but even that they can change first $i= for ($i=1; $i <= $limit; $i++) { to $i=WHATEVER_THE_NUMBER_THAT_SCRIPT_FAILED ... but from my testing it was quite fast for fastly written script. Even if script failed then it will just check some mysql and directories without deleting previously deleted images so it will go quicker on the second, third attempt. You just want to sell your paid solution and that's it because You know that my solution works. Edited December 3, 2021 by hakeryk2 (see edit history) Link to comment Share on other sites More sharing options...
nfuentes Posted May 10, 2019 Share Posted May 10, 2019 On 2/28/2019 at 10:19 AM, hakeryk2 said: Ok, that is it! I was so angry that I can't find any working solution for this so I created my own and it is working. This is the code which You can place in Your file or just download the attached file and put it into main folder of Your shop and run http://yourshop.pl/remove_unused_images.php in Your browser. Things to consider: Change $limit value from 19 line when testing, for example like 100, for production mode use $limit = $counted_images; if scan return 0 or there is an error just check if $scan_dir is ok by adding ddd($scan_dir); arround 11 line of code. Upload script again, test it if scan directory is properly created, if not then change $shop_root and $image_folder values from the beginning of the script, remove ddd part, reupload and run script again. It might take a long time so if You will run out of time then just run the script again. Feel free to not ask about anything because I won't support this code, I am not responsible for it and I won't help You. Likes for this post will be appreciated because there are some paid solutions, modules that are doing basically the same thing. This one is free. This will take some time to process - if You have large ammount of files it can exceed your max_execution_time limit on Your hosting. If this will happen You can try to take parts like 5000 or 10 000 and edit for ($i=1 to start with for ($i=5000 and then restart the script again and again with changing the limit <?php ####PUT THIS FILE INTO YOUR MAIN SHOP FOLDER#### // root path of the shop, almost no one needs to change something here. $shop_root = $_SERVER['DOCUMENT_ROOT']."/"; // need to have slash / at the end $image_folder = 'img/p/'; // also needs slash at the ennd $scan_dir = $shop_root.$image_folder; include_once($shop_root.'config/config.inc.php'); include $shop_root . 'config/settings.inc.php'; #---------------------------------------------# $last_id = (int)Db::getInstance()->getValue(' SELECT id_image FROM '._DB_PREFIX_.'image ORDER BY id_image DESC '); $counted_images = Db::getInstance()->executeS(' SELECT count(*) as qnt FROM '._DB_PREFIX_.'image '); $counted_images = (int)$counted_images[0]['qnt']; echo 'There was '.$last_id.' images in database but only '.$counted_images.' is used right now. Lets check how many of them are eating up our storage without no reason.<br>'; //$limit = 150; // for testing $limit = $last_id; // for production $removed_images = 0; for ($i=1; $i <= $limit; $i++) { if (!imageExistsInDB($i)){ $imageDir = str_split($i); $imageDir = implode('/', $imageDir); $path = $scan_dir.$imageDir; deleteImagesFromPath($path); } } function deleteImagesFromPath($path) { global $removed_images; $images = glob($path . '/*.{jpg,png,gif,jpeg}', GLOB_BRACE); if ($images){ foreach ($images as $file) { if (is_file($file)) { unlink($file); } } $removed_images++; echo 'Deleted images from folder ' . $path . '/' ."<br/>"; } } function imageExistsInDB($id_image){ return Db::getInstance()->getValue(' SELECT id_image FROM '._DB_PREFIX_.'image WHERE id_image = '.(int)$id_image ); } echo '--------------------------------------<br>'; if ($removed_images > 0) echo 'Hurray! We removed '.$removed_images.' product images!'; else echo 'Everything is ok with Your images. I did not removed any of them or I made it before. Good Job Presta!'; remove_unused_images.php Amazing, thanks! 1 Link to comment Share on other sites More sharing options...
xarnoux Posted July 23, 2019 Share Posted July 23, 2019 (edited) Done but I've got this message (PRESTASHOP 1.6.1.17) : Notice: Constant _DB_SERVER_ already defined in /homepages/24/d742654074/htdocs/config/settings.inc.php on line 2 Notice: Constant _DB_NAME_ already defined in /homepages/24/d742654074/htdocs/config/settings.inc.php on line 3 Notice: Constant _DB_USER_ already defined in /homepages/24/d742654074/htdocs/config/settings.inc.php on line 4 Notice: Constant _DB_PASSWD_ already defined in /homepages/24/d742654074/htdocs/config/settings.inc.php on line 5 Notice: Constant _DB_PREFIX_ already defined in /homepages/24/d742654074/htdocs/config/settings.inc.php on line 6 Notice: Constant _MYSQL_ENGINE_ already defined in /homepages/24/d742654074/htdocs/config/settings.inc.php on line 7 Notice: Constant _PS_CACHING_SYSTEM_ already defined in /homepages/24/d742654074/htdocs/config/settings.inc.php on line 8 Notice: Constant _PS_CACHE_ENABLED_ already defined in /homepages/24/d742654074/htdocs/config/settings.inc.php on line 9 Notice: Constant _COOKIE_KEY_ already defined in /homepages/24/d742654074/htdocs/config/settings.inc.php on line 10 Notice: Constant _COOKIE_IV_ already defined in /homepages/24/d742654074/htdocs/config/settings.inc.php on line 11 Notice: Constant _PS_CREATION_DATE_ already defined in /homepages/24/d742654074/htdocs/config/settings.inc.php on line 12 Notice: Constant _RIJNDAEL_KEY_ already defined in /homepages/24/d742654074/htdocs/config/settings.inc.php on line 15 Notice: Constant _RIJNDAEL_IV_ already defined in /homepages/24/d742654074/htdocs/config/settings.inc.php on line 16 There was 34998 images in database but only 6101 is used right now. Lets check how many of them are eating up our storage without no reason. -------------------------------------- Everything is ok with Your images. I did not removed any of them or I made it before. Good Job Presta! Edited July 24, 2019 by xarnoux précision de la version prestashop utilisée (see edit history) Link to comment Share on other sites More sharing options...
Daresh Posted November 2, 2019 Share Posted November 2, 2019 Hi! In case someoune is still looking for an easy solution, I just added a feature to delete all unused images from the img/p folder to the Tidy module. Link to comment Share on other sites More sharing options...
oscarga00 Posted December 6, 2019 Share Posted December 6, 2019 On 2/28/2019 at 7:19 AM, hakeryk2 said: Ok, that is it! I was so angry that I can't find any working solution for this so I created my own and it is working. This is the code which You can place in Your file or just download the attached file and put it into main folder of Your shop and run http://yourshop.pl/remove_unused_images.php in Your browser. Things to consider: Change $limit value from 19 line when testing, for example like 100, for production mode use $limit = $counted_images; if scan return 0 or there is an error just check if $scan_dir is ok by adding ddd($scan_dir); arround 11 line of code. Upload script again, test it if scan directory is properly created, if not then change $shop_root and $image_folder values from the beginning of the script, remove ddd part, reupload and run script again. It might take a long time so if You will run out of time then just run the script again. Feel free to not ask about anything because I won't support this code, I am not responsible for it and I won't help You. Likes for this post will be appreciated because there are some paid solutions, modules that are doing basically the same thing. This one is free. This will take some time to process - if You have large ammount of files it can exceed your max_execution_time limit on Your hosting. If this will happen You can try to take parts like 5000 or 10 000 and edit for ($i=1 to start with for ($i=5000 and then restart the script again and again with changing the limit <?php ####PUT THIS FILE INTO YOUR MAIN SHOP FOLDER#### // root path of the shop, almost no one needs to change something here. $shop_root = $_SERVER['DOCUMENT_ROOT']."/"; // need to have slash / at the end $image_folder = 'img/p/'; // also needs slash at the ennd $scan_dir = $shop_root.$image_folder; include_once($shop_root.'config/config.inc.php'); include $shop_root . 'config/settings.inc.php'; #---------------------------------------------# $last_id = (int)Db::getInstance()->getValue(' SELECT id_image FROM '._DB_PREFIX_.'image ORDER BY id_image DESC '); $counted_images = Db::getInstance()->executeS(' SELECT count(*) as qnt FROM '._DB_PREFIX_.'image '); $counted_images = (int)$counted_images[0]['qnt']; echo 'There was '.$last_id.' images in database but only '.$counted_images.' is used right now. Lets check how many of them are eating up our storage without no reason.<br>'; //$limit = 150; // for testing $limit = $last_id; // for production $removed_images = 0; for ($i=1; $i <= $limit; $i++) { if (!imageExistsInDB($i)){ $imageDir = str_split($i); $imageDir = implode('/', $imageDir); $path = $scan_dir.$imageDir; deleteImagesFromPath($path); } } function deleteImagesFromPath($path) { global $removed_images; $images = glob($path . '/*.{jpg,png,gif,jpeg}', GLOB_BRACE); if ($images){ foreach ($images as $file) { if (is_file($file)) { unlink($file); } } $removed_images++; echo 'Deleted images from folder ' . $path . '/' ."<br/>"; } } function imageExistsInDB($id_image){ return Db::getInstance()->getValue(' SELECT id_image FROM '._DB_PREFIX_.'image WHERE id_image = '.(int)$id_image ); } echo '--------------------------------------<br>'; if ($removed_images > 0) echo 'Hurray! We removed '.$removed_images.' product images!'; else echo 'Everything is ok with Your images. I did not removed any of them or I made it before. Good Job Presta!'; remove_unused_images.php Very nice! thanks works like a charm! 2 Link to comment Share on other sites More sharing options...
selectshop.at Posted December 7, 2019 Share Posted December 7, 2019 I don't know what is getting wrong with your shops, but Prestashop deletes also images automatically when products are deleted from back-office. Perhaps you are not deleting the products from there, or are only hiding them, instead of deleting them. Pay attention, that products already ordered are not deleted, they maintain as copy into database for obvious reasons. 2 Link to comment Share on other sites More sharing options...
hshaker Posted September 24, 2021 Share Posted September 24, 2021 Le 28/02/2019 à 2:19 PM, hakeryk2 a dit : Ok, that is it! I was so angry that I can't find any working solution for this so I created my own and it is working. This is the code which You can place in Your file or just download the attached file and put it into main folder of Your shop and run http://yourshop.pl/remove_unused_images.php in Your browser. Things to consider: Change $limit value from 19 line when testing, for example like 100, for production mode use $limit = $counted_images; if scan return 0 or there is an error just check if $scan_dir is ok by adding ddd($scan_dir); arround 11 line of code. Upload script again, test it if scan directory is properly created, if not then change $shop_root and $image_folder values from the beginning of the script, remove ddd part, reupload and run script again. It might take a long time so if You will run out of time then just run the script again. Feel free to not ask about anything because I won't support this code, I am not responsible for it and I won't help You. Likes for this post will be appreciated because there are some paid solutions, modules that are doing basically the same thing. This one is free. This will take some time to process - if You have large ammount of files it can exceed your max_execution_time limit on Your hosting. If this will happen You can try to take parts like 5000 or 10 000 and edit for ($i=1 to start with for ($i=5000 and then restart the script again and again with changing the limit <?php ####PUT THIS FILE INTO YOUR MAIN SHOP FOLDER#### // root path of the shop, almost no one needs to change something here. $shop_root = $_SERVER['DOCUMENT_ROOT']."/"; // need to have slash / at the end $image_folder = 'img/p/'; // also needs slash at the ennd $scan_dir = $shop_root.$image_folder; include_once($shop_root.'config/config.inc.php'); include $shop_root . 'config/settings.inc.php'; #---------------------------------------------# $last_id = (int)Db::getInstance()->getValue(' SELECT id_image FROM '._DB_PREFIX_.'image ORDER BY id_image DESC '); $counted_images = Db::getInstance()->executeS(' SELECT count(*) as qnt FROM '._DB_PREFIX_.'image '); $counted_images = (int)$counted_images[0]['qnt']; echo 'There was '.$last_id.' images in database but only '.$counted_images.' is used right now. Lets check how many of them are eating up our storage without no reason.<br>'; //$limit = 150; // for testing $limit = $last_id; // for production $removed_images = 0; for ($i=1; $i <= $limit; $i++) { if (!imageExistsInDB($i)){ $imageDir = str_split($i); $imageDir = implode('/', $imageDir); $path = $scan_dir.$imageDir; deleteImagesFromPath($path); } } function deleteImagesFromPath($path) { global $removed_images; $images = glob($path . '/*.{jpg,png,gif,jpeg}', GLOB_BRACE); if ($images){ foreach ($images as $file) { if (is_file($file)) { unlink($file); } } $removed_images++; echo 'Deleted images from folder ' . $path . '/' ."<br/>"; } } function imageExistsInDB($id_image){ return Db::getInstance()->getValue(' SELECT id_image FROM '._DB_PREFIX_.'image WHERE id_image = '.(int)$id_image ); } echo '--------------------------------------<br>'; if ($removed_images > 0) echo 'Hurray! We removed '.$removed_images.' product images!'; else echo 'Everything is ok with Your images. I did not removed any of them or I made it before. Good Job Presta!'; remove_unused_images.php NICE THANKS It work for me 1 Link to comment Share on other sites More sharing options...
bnadauld Posted December 11, 2021 Share Posted December 11, 2021 ive got something wrong when i changed the php file where indicated in the guide. Sorry ive not run a php script before and im working on wamp. Link to comment Share on other sites More sharing options...
Open Presta Posted December 12, 2021 Share Posted December 12, 2021 that is new module for unused image work with all images products, category , cms and css files https://www.myprestastore.com/downloads/clean-delete-unused-image-prestashop/ Link to comment Share on other sites More sharing options...
hakeryk2 Posted December 13, 2021 Share Posted December 13, 2021 On 12/11/2021 at 4:01 AM, bnadauld said: ive got something wrong when i changed the php file where indicated in the guide. Sorry ive not run a php script before and im working on wamp. Leave these variables as they are posted originaly. Just insert script file in the main folder of your shop and run it. You don't need to edit anything and it should run smoothly. 1 Link to comment Share on other sites More sharing options...
bnadauld Posted December 14, 2021 Share Posted December 14, 2021 (edited) On 12/13/2021 at 3:47 PM, hakeryk2 said: Leave these variables as they are posted originaly. Just insert script file in the main folder of your shop and run it. You don't need to edit anything and it should run smoothly. I know you didn't want to support this. But thanks for the help! Edited December 14, 2021 by bnadauld (see edit history) Link to comment Share on other sites More sharing options...
presta_jsM Posted October 18, 2022 Share Posted October 18, 2022 (edited) Hello I modified the script of hakeryk2 to remove duplicate or unassigned images. Here you can check and download the code: https://github.com/melikslab/Clean-unused-images-prestashop/blob/main/cleanImages.php Regards Edited October 21, 2022 by presta_jsM (see edit history) Link to comment Share on other sites More sharing options...
hakeryk2 Posted October 21, 2022 Share Posted October 21, 2022 (edited) Dude... it is just basically my code from few post up here. Edited October 21, 2022 by hakeryk2 (see edit history) Link to comment Share on other sites More sharing options...
presta_jsM Posted October 21, 2022 Share Posted October 21, 2022 (edited) 16 minutes ago, hakeryk2 said: Dude... it is just basically my code. Yes, is based on your code, but yours not clean my duplicated images only search images not asigned in db or slq, now search images where are not asigned to attributes. I cleaned 30GB of my prestashop.🙂 Edited October 21, 2022 by presta_jsM (see edit history) Link to comment Share on other sites More sharing options...
Sasni Posted May 14, 2023 Share Posted May 14, 2023 what if the script wants to remove an unused photo from a directory that doesn't exist? Link to comment Share on other sites More sharing options...
hakeryk2 Posted May 15, 2023 Share Posted May 15, 2023 Prestashop stores image with id 55100 in img/p/5/5/1/0/0 folder. 1 Link to comment Share on other sites More sharing options...
Sasni Posted May 15, 2023 Share Posted May 15, 2023 I know, but for some reason your script was getting stuck like in the picture above. I "wrote" my version -> https://github.com/Sasni/Delete_unused_images/blob/main/foton.php --- Wiem, ale z jakiegoś powodu twój skrypt mi się zacinał jak na obrazku powyżej. "Napisałem" swoja wersje -> https://github.com/Sasni/Delete_unused_images/blob/main/foton.php Link to comment Share on other sites More sharing options...
presta_jsM Posted May 16, 2023 Share Posted May 16, 2023 On 5/14/2023 at 10:31 AM, Sasni said: what if the script wants to remove an unused photo from a directory that doesn't exist? This ID "55100" is product id, not image id... Your script only searches at the '/img/p/3' directory, which will not clean all the images. You should search within the '/img/p/' directory. Link to comment Share on other sites More sharing options...
DeTaker Posted May 30, 2023 Share Posted May 30, 2023 No way, due to those scripts part of my images just dissapeared, is there any way to turn them back 😐 1 Link to comment Share on other sites More sharing options...
Sasni Posted May 31, 2023 Share Posted May 31, 2023 (edited) 20 hours ago, DeTaker said: No way, due to those scripts part of my images just dissapeared, is there any way to turn them back 😐 Where is your backup? There you will find your photos. Also, what script did you use? On 5/16/2023 at 5:34 PM, presta_jsM said: Your script only searches at the '/img/p/3' directory, which will not clean all the images. You should search within the '/img/p/' directory. Ok, I corrected the script to include all directories from 1 to 9.https://github.com/Sasni/Delete_unused_images/blob/main/foton.php Edited May 31, 2023 by Sasni (see edit history) Link to comment Share on other sites More sharing options...
psy_ch Posted August 3, 2023 Share Posted August 3, 2023 (edited) What are you people doing on your shops when you have literally no clue about nothing... one even lost images because of no backups. that's what i call comedy :'D There's people out there called web-devs... maybe try those once in a while.. I mean you're all dealing with a script that deletes stuff and you do not consider getting a clue before you hit any delete buttons or run any scripts. scripts you obviously do not even understand!!! what if a scripts sends all your database info to the scripts author? You would just run them.. hilarious .. and the best of you probably even save credit card or other data in their shops.. unbelievably tragic behavior! I truly hope you people have no customers! Edited August 3, 2023 by psy_ch (see edit history) 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now