How to Delete Products Programmatically in Magento.?

Deleting products in Magento:-

If you are looking to delete the products in Magento then you must do it programmatically with script as per the catalog. Before starting the whole code acquire the right store id as you might have multiple store’s in Magento respectively. You can choose to delete single or Bulk products depending on the requirement.


Magento Delete Single Products:-

With below code you can load the product by id and delete it Programmatically .

<?php
Mage::register("isSecureArea", 1);
$productid = 10; //use your own product id
try{
    Mage::getModel("catalog/product")->load($productid)->delete();
}
catch(Exception $e)
{
    echo "Delete failed";
}
?>
Above script first load the single product and then delete it simultaneously in Magento.

Magento Delete All Products Programmatically-:

To delete all products in Magento programmatically with script we are going to use Magento Mage calls. This method uses Mage class and removes every product in the Magento catalog respectively. This method is faster than manually deleting the products from admin. Re-confirm the store id once again and start the process for deleting all products in Magento with following steps.

Step 1:- Create a new file with name deleteproducts.php at root of the all files.
Step 2:- Paste the following precise code into deleteproducts.php
Step 3:- Run file using the url : http://yourexampledomain.com/deleteproducts.php

set_time_limit(0);  // Set Time limit
ini_set('memory_limit','512M');  // Memory Limit
      
  require_once './app/Mage.php';  
   Mage :: app("default")->setCurrentStore( Mage_Core_Model_App :: ADMIN_STORE_ID );  
     //$storeId = 1;     
    $products = Mage :: getResourceModel('catalog/product_collection')
    ->setStoreId(1)->getAllIds();  
    if(is_array($products))  
    {  
        foreach ($products as $key => $productId)  
        {  
            try  
            {  
                $product = Mage::getModel('catalog/product')
               ->load($productId)->delete();  
                echo "Successfully deleted Product With ID: ". $productId."<br>";  
            }   
            catch (Exception $e)   
            {  
                echo "Could not delete Product With ID: ". $productId."<br>";  
            }  
        }  
    }

The above script will delete all products from magento.

You may also like...

Leave a Reply

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