Hi,
how can we cleanup delete old unlinked images media in virtuemart
best regards
Ocean
I have been looking for an answer to this for a while. A client decided to change his mind about which images to use... a few times... on a few hundred products. Now everything is a mess.
After a few days I still can't find any solution.
Ok. In reality you would like to see an option that says what photos or images are currently being used in the store and what images are not being used and then maybe delete the rest.
I can see in a large store that would be a real pain.
In a small store most would use FTP to download all the media files both original and thumbnails and then go trough the list of products . Then syncronise them back saying delete those not on file.
In VM under tools there is a syncronise option for media. This will make sure that the product are all linked correctly.
If you upload and see an image missing then syncronise again.
Now there are thrird party extensions that you can use to upload large volumes of products .. This could be an option .. You need to investigate this.option.
I did do a similar option once where all products were alway placed into a category call all products. I hid this category from the public but it allowed me to turn it on and look at all the products quickly. I could see what products has missing images and what products may have changed. I know now this might take a little time but for future reference it helps with similar situations.
Try this solution:
<?php
$type = 'product'; // type of image: "product" or "category"
$imgPath = $_SERVER['DOCUMENT_ROOT']."/images/stories/virtuemart/$type/"; // default path to full-sized images
$resizedPath = $_SERVER['DOCUMENT_ROOT']."/images/stories/virtuemart/$type/resized/"; // default path to thumbnail images
set_time_limit(0);
require $_SERVER['DOCUMENT_ROOT'].'/configuration.php';
$conf = new JConfig;
$db = new mysqli($conf->host, $conf->user, $conf->password, $conf->db);
$images = array_slice(scandir($imgPath), 2);
$resized = array_slice(scandir($resizedPath), 2);
$query="SELECT
virtuemart_media_id,
file_url,
file_url_thumb
FROM {$conf->dbprefix}virtuemart_medias
WHERE virtuemart_media_id IN(SELECT DISTINCT virtuemart_media_id FROM {$conf->dbprefix}virtuemart_{$type}_medias)";
$res = $db->query($query);
if (!$res) die("Cannot get database resource!");
while($r = $res->fetch_array()){
$activeID[] = $r[0];
$p = pathinfo($r[1]);
$activeImg[] = $p['basename'];
$p = pathinfo($r[2]);
$activeResized[] = $p['basename'];
}
if (!is_null($activeImg)){
$notActiveImg = array_diff($images, $activeImg);
};
if (!is_null($activeResized)){
$notActiveResized = array_diff($resized, $activeResized);
}
if (!is_null($activeID)){
$db->query("DELETE FROM {$conf->dbprefix}virtuemart_medias WHERE file_type='$type' AND virtuemart_media_id NOT IN(".implode(',', $activeID).")");
}
$db->close();
$full=0;
$thumb=0;
if (!is_null($activeImg)){
foreach($notActiveImg as $i){
if(is_file($imgPath.$i) && $i != 'index.html'){
if (unlink($imgPath.$i)) $full++; else echo "Cannot delete $imgPath$i<br>";
} else "File not exists $imgPath.$i<br>";
}
}
if (!is_null($activeResized)){
foreach($notActiveResized as $i){
if(is_file($resizedPath.$i) && $i != 'index.html'){
if (unlink($resizedPath.$i)) $thumb++; else echo "Cannot delete $imgPath$i<br>";
} else "File not exists $imgPath.$i<br>";
}
}
if ($full>0) echo "Deleted $full full-sized images<br>";
if ($thumb>0) echo "Deleted $thumb thumbnail images<br>";
if ($full==0 && $thumb==0) echo "Nothing to delete";
?>
tested on Virtuemart 4 and Joomla 4
works
Thanks :-)
this freebie also works well
https://shop.st42.fr/en/products/virtuemart-media-folder-clear.htm