Sunday, 15 March 2015

magento Delete all Products Programmatically

Hello Friends

In this article, we’ll be using a simple method using our Mage class, and removing products via the Magento Mage calls. Using this will result in removal of every product in your Magento catalog, so be careful ! Be sure to modify store id because we may have multiple stores in magento at a time.

This method works 10 times faster than basic products delete from admin.
1) Create new directory “custom” in root magento directory and create new file : deleteProducts.php
2) Paste the following code into deleteProducts.php
3) Run file using url : http://yourmagentostore.com/custom/deleteProducts.php


set_time_limit(0);
ini_set('memory_limit','512M');
   
require_once '../app/Mage.php';
Mage :: app("default")->setCurrentStore( Mage_Core_Model_App :: ADMIN_STORE_ID );
       
    $products = Mage :: getResourceModel('catalog/product_collection')->setStoreId(1)->getAllIds();
    if(is_array($products))
    {
        foreach ($products as $key => $pId)
        {
            try
            {
                $product = Mage::getModel('catalog/product')->load($pId)->delete();
                echo "successfully deleted product with ID: ". $pId ."
";
            }
            catch (Exception $e)
            {
                echo "Could not delete product with ID: ". $pId ."
";
            }
        }
    }


Let me know if you have any query

Enjoy

4 comments:

  1. Nice job, working for version 1.8 ! Thank you ;)

    ReplyDelete
  2. Great job again!

    For some reason it stops at about 50 products and I would have to enter the address in the url again to get it to starting again.

    Any idea why?

    ReplyDelete
  3. Intersting. Does it delet all the relations towards the product as well?

    ReplyDelete
  4. Thanks for sharing this Information, Got to learn new things from your Blog on MAGENTO.
    Ref link : http://thecreatingexperts.com/magento-training-in-chennai/

    ReplyDelete