0

Is there a way to delete all images for products that are set as disabled?

It's just a lot of my products that are disabled are taking my storage space up on my server.

I'd just like to be able to cron a PHP script every week or so however I'm unsure what this script might actually look like?

Any idea if this is possible?

1 Answer 1

2
<?php

//This method will work if you hit the file on root. There can be many rectifications for this. But please make sure to take proper backup before running this
set_time_limit(0);
error_reporting(E_ALL ^ E_NOTICE);
ini_set("display_errors",'On');

require_once 'app/Mage.php';
umask(0);
Mage::app('default');

$products = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('*')
    ->addFieldToFilter('status',Mage_Catalog_Model_Product_Status::STATUS_DISABLED);

$products->load();
foreach($products as $product) {

$id = $product->getId();
$product = Mage::getModel('catalog/product')->load($id);
$mediaGalleryAttribute = Mage::getModel('catalog/resource_eav_attribute')->loadByCode($entityTypeId, 'media_gallery');
$gallery = $product->getMediaGalleryImages();
foreach ($gallery as $image)
    $mediaGalleryAttribute->getBackend()->removeImage($product, $image->getFile());
$product->save();

}

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.