Jump to content

Removing unused images


Syns

Recommended Posts

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

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

  • 3 years later...

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 by hakeryk2 (see edit history)
  • Like 4
  • Thanks 5
  • Haha 1
Link to comment
Share on other sites

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 by hakeryk2 (see edit history)
Link to comment
Share on other sites

  • 2 months later...
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!

  • Thanks 1
Link to comment
Share on other sites

  • 2 months later...

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 by xarnoux
précision de la version prestashop utilisée (see edit history)
Link to comment
Share on other sites

  • 3 months later...
  • 1 month later...
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!

  • Like 2
Link to comment
Share on other sites

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.

  • Like 2
Link to comment
Share on other sites

  • 1 year later...

 

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

  • Like 1
Link to comment
Share on other sites

  • 2 months later...
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.

  • Like 1
Link to comment
Share on other sites

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!

Untitled picture.png

Untitled picture2.png

Untitled picture3.png

Edited by bnadauld (see edit history)
Link to comment
Share on other sites

  • 10 months later...
  • 6 months later...
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?

image.thumb.png.80ce02a170cc85d0219b08987de8d792.png

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

  • 2 weeks later...
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 by Sasni (see edit history)
Link to comment
Share on other sites

  • 2 months later...

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 by psy_ch (see edit history)
  • Sad 1
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...