Bit of a wierd one this and haven't got the bottom of it yet, nor have I seen it before. So I'm setting up the prod server of a new store and when deploying to the server, the first time bin/magento is run, even without any arguments, just bin/magento on it's own, everything under generated/code and generated/metadata gets deleted. Worth noting that with all further runs of bin/magento the files aren't deleted. I'm using deployer.org to deploy which isn't relevant apart from that it makes releases for each deployment so I believe that as far as Magento is concerned when bin/magento is run after a simple git clone and then composer install it's the first time the command has been run. The config in env.php sets the mode to production

Can anyone shed any light on what is going on exactly? I have deployed plenty with a closely equivalent method for other stores and never come across this oddity. I also can't seem to find any examples of tickets, threads etc of others having this same issue.

1 Answer 1


Hopefully useful to others who may come across the same issue, but I've managed to track down the issue. The culprit is the magento-hackathon/magento-composer-installer module. This module has a method MagentoHackathon\Composer\Magento\Plugin::requestRegeneration() which creates the empty file var/.regenerate. This method is called in 2 other plugin methods in the class being onPackageUnistall() and onNewCodeEvent().

The reason the files get deleted is because every time the object manager is instantiated in Magento\Framework\App\ObjectManagerFactory::create() there is a call to Magento\Framework\Code\GeneratedFiles::cleanGeneratedFiles(). This method checks for the existence of the file var/.regenerate and if it exists the generated/code, generated/metadata and var/cache directories are cleared. After this the method then deletes the var/.regenerate file hence the problem only showing itself on 'first run'.

I can understand the principle behind the magento-hackathon/magento-composer-installer logic, but it's been badly implemented really and doesn't consider the use case of deployments to a new release directory, or even if the deploy mode is production.

  • Thanks for this interesting explanation. How did you actually solve this issue?
    – Simon
    Jul 30, 2020 at 8:46
  • 1
    I created a custom maintenance page so requests don't even hit Magento when in maintenance mode (devdocs.magento.com/guides/v2.4/comp-mgr/trouble/cman/…) and then put the store in maintenance mode as composer install runs, disabled cron too as that can kick off a request doing the same thing and then deleted var/.regenerate before taking the store out of maintenance mode. Aug 4, 2020 at 6:31

Your Answer

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

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