Magento delete products programmatically – part 1

//Magento delete products programmatically – part 1

Magento delete products programmatically – part 1

One of our clients has recently contacted us with the following task:
he needed a database backup that would contain products from just one specified category.
So we had to copy the database and delete all the products that didn’t belong to that category.
It’s necessary to mention that his Magento store is quite big and has more than 500K of products, so deleting unnecessary products could be a really time consuming task.

The most frequent advice you might find on the Web is to use Magento catalog/product model to delete products, and that actually was the first idea that came to my mind.

So I’ve taken out the ids of the products that should be kept and wrote a pretty straightforward php script to delete them. The idea was to get minimum and maximum product ids and run a cycle checking if id shouldn’t be kept then we could delete it. (the ids went mostly one by one so I thought not to load product collection to save system resources)

Having run that file from console I saw that though it was deleting products pretty fast, still it would take about 3-4 hours to delete them all, so I just wondered how to speed up the process.

If you’ve searched how to delete products in Magento programmatically on the Internet, you might have seen that most advisers say you shouldn’t do it via mysql. Let’s find out why.

I hope you know that calling Mage::getModel(“catalog/product”) would return an instance of Mage_Catalog_Model_Product (if there were no rewrites done). So let’s find the delete() method.
Hint for beginners: Mage_Catalog_Model_Product source code could be found in app/code/core/Mage/Catalog/Model/Product.php file

Here it is:

I guess you know (otherwise I don’t think you would be reading this) that parent::delete() calls this method from the parent class. As class Mage_Catalog_Model_Product extends Mage_Catalog_Model_Abstract let’s try to find delete() method there. However I don’t think you will find it there as this method is being inherited from Mage_Core_Model_Abstract.

Here is the code:

here is the piece of code that we need

it calls resource model for our Mage_Catalog_Model_Product class. To find what class is called we need to open config.xml file under app/code/core/Mage/Catalog/etc

so Mage_Catalog_Model_Resource is the prefix and we need to take a look at Mage_Catalog_Model_Resource_Product class. However you won’t find delete method there as again it is being inherited from Mage_Eav_Model_Entity_Abstract

To be continued…

By | 2017-12-21T12:11:31+00:00 November 11th, 2015|Magento|2 Comments

About the Author:

2 Comments

  1. l3bel March 23, 2016 at 11:28 am - Reply

    Hello,
    Great post! Part 2 pleaaase 😉
    In the first example code, the check in line 15 should be inverted ( !in_array…. )

    • Admin March 23, 2016 at 12:58 pm - Reply

      Corrected, thank you. Unfortunately still have no enough time to finish part 2, however hope to do it soon

Leave A Comment