Jump to content

How to remove images that are not anymore exist in products


Recommended Posts

Hello everyone, I'm looking a way to delete images from the server which are not anymore exist in my product list. I can sit down and delete manually one by one but this method it will take ages to finish. Is there any faster way? Also when I delete a product is there any way that I can delete automatically the related images of this product?

 

Thank you

Link to comment
Share on other sites

  • 1 month later...

I've prepared a script.

 

The script will read the img/p folder and check for each file if it exists in the database table ps_image.

In case it doesn't exists then it prints the file and deletes the file. I commented the unlink() so you can run it without really deleting and when you are confident then you can uncomment the "unlink()" command and it will really delete the image files. 

 

You need to configure the root of the shop path. And you can set a max number of files that the script will read from the img/p folder. 

 

The script is not yet prepared for the new img/p/1/2/3/ structure, only for the structure to have all jpg files directly in the img/p. It should be relative easy to improve this.

 

Copy this script into a file "cleanup.php" in the root of your shop. Then edit the value $shop_root and the run the script by the browser (http://yourdomain.com/cleanup.php) or on the console (login with ssh for the experts). 

 

The query to check the ps_image table will always result in 1 row to ensure that no files will be deleted when there are database errors. So the script is secured for unexpected mysql errors (like overload of mysql server which you don't want to result in not finding the images and thus would remove the image files).

<?php
// root path of the shop
$shop_root='/home/kooozybe/public_html/shop/';
// limit number of image files to check, set to 10 for testing
$limit=1000000;


include $shop_root . '/config/settings.inc.php';
$pdo = new PDO( 'mysql:host='._DB_SERVER_.';dbname='._DB_NAME_, _DB_USER_, _DB_PASSWD_ );
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$r=$pdo->query('select count(1) cnt from ps_image')->fetch();
echo 'count images database: '.$r['cnt'];

// reset some counters
$cnt_files=0;
$cnt_checked=0;
$cnt_not_found=0;
$cnt_found=0;
$path=$shop_root.'/img/p';
if ($handle = opendir($path)) {
    while ($cnt_files != $limit && false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != "..") {
                $cnt_files++;
                $pi = explode('-',$entry);
                if($pi[0]>0 && $pi[1]>0) {
                        $cnt_checked++;
                        if(!checkExistsDb($pdo,$pi[0],$pi[1])) {
                                $cnt_not_found++;
                                echo 'rm '.$path.'/'.$entry."\r\n";
// this will delete files                                unlink($path.'/'.$entry);
                        } else {
                                $cnt_found++;
                        }
                }
        }
    }
    closedir($handle);
}

echo 'files: '.$cnt_files.' checked: '.$cnt_checked.' not_found: '.$cnt_not_found.' found: '.$cnt_found;

function checkExistsDb($pdo, $id_product, $id_image) {
        $r=$pdo->query($q='select \'ok\' ok, (select id_image from ps_image where id_product = '.(int)$id_product.' and id_image = '.(int)$id_image.') id_image');
        $row=$r->fetch();
        if($row['ok']!='ok') die( 'Problem with query, please correct');
        return $row['id_image']?true:false;
}

I hope this will help you

Edited by shopimport.nl (see edit history)
  • Like 1
Link to comment
Share on other sites

  • 7 months later...

Thank you for your Script, but I had still upgraded to the new img/p/1/2/3/ structure. 

But study the structure a bit, I think I got the point...

 

In /img/p/ there are all the main-Image Files at the Shop - named 2228.jpg, 2229.jpg and so on.

 

For each File, which product still exists in the Shop, there are subfolders. You got the right Subfolder, if you add a / after each single digit of the filename.

 

For 2228.jpg, you found your Files in /img/p/2/2/2/8. There you have one file, for each Image-Configuration you have set active in the Backend (Settings / Images).

 

My theroy: If you parse the img/p/ folder for each "MAIN-jpg"-File, and check, if it appear in the Database, it still exist. If the File isn't in the Database, remove it from your image-Folder.

After clean all the files which are deprecated, rebuild your images via the Backend (Settings/Images/Reset Thumbs)

 

Maybe somebody can adjust the script for that.

Link to comment
Share on other sites

  • 8 months later...

Hello, does anyone have reversed variant of this script? Our product supplier has problems with image distributions in import.. (they didnt have uploaded lots of images, but still give URLS for them in export.....), we have edited import, so if URL is invalid, we save some default img.. but we still have lots of old products with invalid imgs.. indexed images, but not in filesystem..

Regenerating images doesnt work..

 

 

Please i will be realy grateful for some help.

Link to comment
Share on other sites

×
×
  • Create New...