I have created Data Patch, debugged it and everything works as expected, products are removed from the collection but after running setup upgrade changes are not made in the database, what can be cause of the error ? My code is below



namespace DevAll\ExtensionSettingConfig\Setup\Patch\Data;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\StateException;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Api\AttributeRepositoryInterface;
use Magento\Catalog\Model\ProductRepositoryIn;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\App\Area;
use Magento\Framework\App\State;
class RemoveLGManufacturer implements DataPatchInterface
    private $eavSetupFactory;

    private $attributeRepository;

    private $_productRepositoryInterface;

    private $collectionFactory;

    private $state;

    private  $productCollectionFactory;

    private $productRepository;

    private $searchCriteriaBuilder;
    public function __construct(
        State $state,
        CollectionFactory $collectionFactory,
        SearchCriteriaBuilder $searchCriteriaBuilder,
        AttributeRepositoryInterface $attributeRepository,
        ProductRepositoryInterface $productRepositoryInterface,
        array $data = []
    ) {
        $this->state = $state;
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
        $this->_productRepositoryInterface = $productRepositoryInterface;
        $this->attributeRepository = $attributeRepository;
        $this->collectionFactory = $collectionFactory;

     * @inheritDoc
    public static function getDependencies()
        return [];

     * @inheritDoc
    public function getAliases()
        return [];
    public function apply()
          //$searchCriteria = $this->searchCriteriaBuilder->addFilter('manufacturer','LG')->create();

        $productcollection = $this->collectionFactory->create()->addAttributeToSelect('manufacturer','LG')->load();
        foreach ($productcollection as $product) {
            try {
            } catch (StateException $e) {
        return $productcollection;


1 Answer 1


removeItemByKey does not delete a product from the database. It just removes it from the product collection object.
If you want to remove products you can use

  • I was using that before but it returns exception message like The "1449197175" product couldn't be removed. What do you think How can I solve this problem ? Thanks for answer. Mar 28, 2020 at 18:25
  • Not sure exactly what the problem might be. Try debugging. But one thing is clear. RemoveByKey does dot delete data from db
    – Marius
    Mar 28, 2020 at 18:58
  • replaced it with $this->_productRepositoryInterface->delete($product); but it returns that exception when debugging nothing more ... Yeah maybe I am stucked :D Mar 28, 2020 at 19:17
  • 1
    I think it can be interesting for u . I setted $state->setAreaCode('frontend') ; and It started working . thanks. Mar 29, 2020 at 15:18

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.