I can't find any information on how to programmatically re-save all products.

I'm running into a weird error that suddenly all products with configurable options, the options no longer show up on the frontend unless I manually go into the backend and click save on the product.

I have over 1000 products so it would be really helpful if I could get a script working to programatically re-save all products so that their options show up on the frontend again.

Things I have already tried:

  1. reindex all by php bin/magento indexer:reindex
  2. cleared all cache etc
  3. redeployed static assets
  4. ran php bin/magento setup:upgrade etc...

If someone could convert this logic to Magento 2 that would be soooo helpful and save me probably 10 hours of work:


"Due to index problem in Magento which causes the product information isn’t shown correctly on frontend. You will have to resave the product to have correct data again. But there are a lot of products you can do it manually. Here is script to help you to save all products quickly.

Step 1 : Create file named “resaveallproduct.php” in root folder of magento. Step 2 : Paste following code."

// require magento core
require_once 'app/Mage.php';

// execute on admin store

$products = Mage::getModel('catalog/product')->getCollection();
foreach($products as $product) {
    echo $product->getName() . '<br/>';
    // save the product

echo 'Congratulations!!! Product resaved';

Step 3 : Now run “resaveallproduct.php” file from broswer. For Example : yourdomain/resaveallproduct.php

I need the above code basically to work for magento 2, or any other type of workaround.

Please help!


I think the best would be to use an UpgradeData script. First, you will need to create a module - which is pretty easy (or see the example files below). You only need to create the registration.php and etc/module.xml files and then run:

  • bin/magento module:enable --clear-static-content Component_Name
  • bin/magento setup:upgrade

After that, create an UpgradeData class in a setup directory. There you will need to use constructor dependency injection for the product collection and the state. After you have those in your class, setup the upgrade method to do the work.

Note: you will need to change the setup_version in your module.xml any time that you want the script to run.

Here's an example (in my test module SwiftOtter\ShareButtons):

namespace SwiftOtter\ShareButtons\Setup;

use Magento\Framework\App\State;
use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\Framework\Setup\{UpgradeDataInterface, ModuleDataSetupInterface, ModuleContextInterface};

class UpgradeData implements UpgradeDataInterface
    protected $productCollection;
    protected $state;

    public function __construct(Collection $productCollection, State $state) {
        $this->productCollection = $productCollection;
        $this->state = $state;

    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)

        $products = $this->productCollection->load();
        $i = 0;

        foreach ($products as $product) {
            if ($i > 4) {



A few notes on that:

  1. That is using PHP7 for the use statements so you will need to adjust the curly braces if you are running 5.6.
  2. I left the iterator in there so you could easily test just the first couple of products in your store to ensure it works correctly.

If it helps, here's what the etc/module.xml is (after you change the name):

<?xml version="1.0" ?>
    <module name="SwiftOtter_ShareButtons" setup_version="0.1.7" />

And registration.php:

use \Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::MODULE, 'SwiftOtter_ShareButtons', __DIR__);
| improve this answer | |
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');


foreach($productCollection  as $product)





| improve this answer | |

The easiest way I can think of is the following:

Create a new product attribute, attribute name and attribute code are irrelevant, just make sure it is not visible on the frontend.

Any time you want to re-save all products, go to Products -> Catalog grid, select all products and click on 'Update Attributes' from the Actions dropdown (top left of the grid).

Change the value for your new attribute and save. If this takes too long or if you run into timeouts you can also do this page-by-page. You can set the page size to any size you want (e.g. batches of 500).

| improve this answer | |

You can do this by using the ProductRepository:

$filters = $this->searchCriteriaBuilder->create();
$products = $this->productRepository->getList($filters);

foreach ($products as $product) {
     echo $product->getName() . "\n";

| improve this answer | |

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.