2

Main Question

How can I delete all products within a specific category?

Detailed Version: My goal is to remove the products and product images completely from my store inventory. I plan to use the products' category as a way to target the products for removal.


Secondary Questions

Built Into Magento? Does Magento offer this feature within the control panel?

Third-Party GUI? Does a third-party GUI for this feature exist?

Third-Party Script? Does a third-party script for this feature exist?


What I've Found

I found a third-party PHP script, that could be a solution. But the blog post was published in 2011, so I don't know if the PHP script will work with the current 1.9 series, of the Magento Community Edition.

This is the third-party PHP script I found: https://oldwildissue.wordpress.com/2011/11/21/delete-all-the-products-in-a-given-category-via-php/

8
  • In any case I advise you to do a backup first. You can start by doing a database backup and trying the script you posted which seems usable at a first glance from my phone and if it does not work you can restore the backup you made first.
    – mbalparda
    Commented Jun 21, 2015 at 22:32
  • 1
    A simple database query will do the trick DELETE FROM catalog_category_product WHERE category _id = yourCatId; Commented Jun 22, 2015 at 0:15
  • mbalparda: Thanks the backup advice, I'll keep it in mind. === @TimHallman: Thanks for the idea. If you could elaborate on where that query would be used (a terminal command for MySQL?) and clarify if the command will also remove product images, it would make a great answer to add below.
    – Ademos
    Commented Jun 22, 2015 at 0:53
  • 1
    There are multiple ways to execute a query, but you could simply use phpmyadmin. The question is perhaps too broad. Commented Jun 22, 2015 at 1:05
  • @TimHallman: Thank you for the clarification. It's true that there are multiple methods of executing MySQL queries, but it's my understanding that some MySQL query methods are better suited for certain contexts. For example, when removing products from a category on a shared server, PHPMyAdmin would be be limited in execution time, compared to using MySQL terminal commands. Perhaps you could offer advice on that in your answer. === Also, after the products were removed, would the related product images be removed too? If not, does a method exist to remove both database entries and images?
    – Ademos
    Commented Jun 22, 2015 at 2:45

3 Answers 3

1

You can add button to category edit page like in this tutorial. And send ajax request to controller which remove all assigned products.

Use loaded category for get all assigned products and just remove it.

$productCollection = $category->getProductCollection();
foreach ($productCollection as $product) {
    $product->isDeleted(true);
}
$productCollection->save();

For remove all assigned images use the function below

public function removeProductImages($product)
{
    if ($product->getId()) {
        $mediaApi = Mage::getModel("catalog/product_attribute_media_api");
        $items = $mediaApi->items($product->getId());
        foreach ($items as $item) {
            $mediaApi->remove($product->getId(), $item['file']);
        }
    }
}

Just modify first script. And as a result you have:

$productCollection = $category->getProductCollection();
foreach ($productCollection as $product) {
    $product->isDeleted(true);
    $this->removeProductImages($product);
}
$productCollection->save();
2
  • Thank you for sharing that method of adding a button to the category edit page, to remove all assigned products. However, my goal is to remove the products and product images completely from my store inventory. I plan to use the products' category as a way to target the products for removal. === If a button could be added that would allow me to perform the action I mentioned, that might be a solution.
    – Ademos
    Commented Jun 23, 2015 at 5:18
  • See my answer update
    – Neklo.com
    Commented Jun 23, 2015 at 6:46
1

The question is really too broad. As others have commented, you can delete products that are within a specific category via the database. And not only commented, you linked a code-example as well which you can turn into a simple shell-script. Due to references, deletions should populate properly.

Flush cache and re-index afterwards.

Naturally removing data from the database won't remove images on disk. As this has never been the case (and Magento only knows about some of those images anyway when your turn it around a little bit), so you're not the only one with the problem. You could just take an existing script, like (only to give one example):

$ n98-magerun.phar media:images:removeorphans

This is part of a Magerun third party module named magerun-addons (by Peter Jaap Blaakmeer).

0

You can do this from the admin panel.

Catalog -> Manage Categories -> Select your category -> Select "Category Products" then uncheck the checkbox found in the grid header and save.

1
  • Thanks for your answer. However, it seems there was some confusion. Your answer would allow me to remove the products from the category, but I am looking for a way to remove the products and product images completely from my store inventory. I plan to use the products' category as a way to target the products for removal. === Sorry for the confusion, I edited my question to clarify my goal.
    – Ademos
    Commented Jun 23, 2015 at 5:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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