0

I have a lot of products that are part of disabled categories. Is there a way for me to delete them all?

I want to tell it the ID of the category and then delete all the products under that category.

Is this possible?

Thank you very much!

5
  • delete products or unassign products from the category ? Jul 19, 2016 at 14:47
  • delete products!
    – PET
    Jul 19, 2016 at 14:59
  • check my updated answer which deletes products permanently from magento of disabled categories Jul 19, 2016 at 15:10
  • Worked for you ? Jul 19, 2016 at 16:22
  • I will try it tonight.
    – PET
    Jul 20, 2016 at 8:13

2 Answers 2

1

Replace @categoryId = 5 with your category id.

SET @categoryId = 5;  
DELETE FROM `catalog_product_entity` 
where `entity_id` IN 
 (select `product_id` from `catalog_category_product`
where `category_id` = @categoryId);

Note: Take backup first before hitting any direct query on DB.

2

to delete permanently products of disable categories use following code:-

<?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);
umask(0);

$category_id = 45;
$products_collection = Mage::getModel('catalog/category')->load($category_id)->getProductCollection();

foreach($products_collection as $product){
    try{
        $product->delete();
    }catch(Exception $e){
        Mage::log("Unable to delete product with ID: ".$product->getId().");
    }
}

Note:- If your product which is under disabled category as well as enabled category then product as product is getting deleted the product will be gone from the enable category as well

1
  • Thank you Pradeep for the answer. It's easier for me to delete this using SQL and not a PHP file... so I have to chose the other answer... However, is there a way to automatically search for all disabled categories and then delete the products from those categories? This category_id thing is great, but I do have a lot of categories. Thank You
    – PET
    Jul 20, 2016 at 19:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.