Mass delete magento products based on product id range

This tutorial explains how to mass delete magento products based on product id range. Using this script you can delete the products within the products id.

Please also refer the tutorial that explains how to mass delete all the products and categories from the magento site using the script.

<?php
require_once '../app/Mage.php';
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));

$fromStart = 577;
$toEnd = 771 ;

removeAllProducts($fromStart,$toEnd);

echo 'DONE'.PHP_EOL;
exit();

function removeAllProducts( $fromStart ,$toEnd ){

    $products = Mage::getModel('catalog/product')->getCollection()
        ->addAttributeToSelect('*')
        ->addAttributeToFilter('entity_id', array(
            'from' => $fromStart,
            'to' => $toEnd
        ))
        ->load();

        foreach ($products as $product)
        {

            try
            {
                Mage::getModel('catalog/product')->load($product->getId())->delete();
                echo 'Product Removed '.$product->getId() .PHP_EOL;

            }
            catch (Exception $e)
            {
                echo 'Failed to Remove product '.$product->getId() .PHP_EOL;
            }
        }
}

Please also refer the tutorial that explains how to mass delete all the products and categories from the magento site using the script.

Share this Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *

*