Categories: Magento

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.

Rohan pathak

Recent Posts

Modern Toys, Magical Moments: Why the Best Toy Shop in Noida Is More Than Just a Store

When it comes to children, there’s one universal truth: the right toy can spark imagination, build skills, and make memories…

6 months ago

Rediscovering Joy: A New Era of Creativity & Comfort in Toy Stores

In today’s digital age, where screens and gadgets dominate our children’s lives, there’s something heartwarming about a well-loved plush toy…

6 months ago

Unboxing Imagination: Discovering the Joy of Play at a Toy Store in Noida

In a world dominated by screens and fast-paced routines, it’s easy to forget the simple magic of a toy in…

6 months ago

Imagination Unboxed: Discover Joy at the Toy Shop in Delhi

In the heart of Delhi’s vibrant streets lies a world where imagination meets innovation — the magical universe of toys.…

6 months ago

Play with Purpose: Discovering the Ultimate Toy Store in Noida

When was the last time a toy truly amazed you—not just as a product, but as a thoughtful tool for…

6 months ago

From Tears to Toys: Exploring Modern Childhood through Delhi’s Favorite Toy Shop

In the digital age, the way we experience childhood has changed, but the essence remains the same—imagination, exploration, and joy.…

6 months ago