I was trying to install a plugin and I removed var/generation and var/cache/*. I've checked the MAGE_MODE and I'm in developer but now when I access the front-end and the admin I get random errors saying mean things like: Class Magento\Customer\Model\CustomerFactory does not exist

Class Magento\Sales\Model\ResourceModel\Report\OrderFactory does not exist

Some of the classes are being created, but not all. I've tried clearing cache again, and the generation folder but then I just get the same errors.

Any ideas?

  • 1
    check the rights on the var/generation folder
    – Marius
    Commented Feb 24, 2016 at 16:08
  • it says drwxrwxrw- 5
    – jstrez77
    Commented Feb 24, 2016 at 16:16
  • I had to clean the var/generation folder a few times, go to the home page first, the clean, then admin, then login, then clean... and it worked...?? I don't understand this
    – jstrez77
    Commented Feb 24, 2016 at 17:00
  • Definitely permissions within 'var/generation' folder. Commented Jul 20, 2017 at 15:33

7 Answers 7


Magento 2 generates Factory classes inside var/generation directory. So, if there is folder permission issue or folder owner issue with that directory then the factory class cannot be generated and you get such error.

When you clear var/generation directory and set appropriate permission to it then this error should be solved.

See more on Code generation in Magento 2

  • 1
    Could be also var/di or var/cache, see devdocs.magento.com/guides/v2.2/install-gde/trouble/…
    – Yvan
    Commented Sep 28, 2018 at 10:44
  • Funny how it always come back to basics. Lol, I need to stop having denials. Commented Sep 18, 2020 at 4:14
  • @Mukesh Chapagain Code generation in Magento 2 link is not working. Please add the appropriate link. Commented Jul 12, 2022 at 11:54
  • @SavanDholu Updated the answer with a working link. Thanks. Commented Aug 7, 2023 at 21:11

For me, i removed var/generation folder then i ran magento setup:di:compile which generated all necessary classes.

(in my case : Then i was encountered with cache issue, i removed initial cache folder.)

Then it solved my issue. Cheers !!

  • 1
    needed to change files owner at the end Commented Oct 9, 2016 at 20:45

In magento new versions like 2.2.2 and above this code generation path changed to

<Magento root folder>/generated

so you need to check permissions for this folder as well. You need to run following command at the root of Magento installation directory will resolve this issue:

sudo chmod -R 777 generated/
  • clear cache as well in start Commented Sep 3, 2018 at 8:13
  • its work for me
    – matinict
    Commented Sep 24, 2018 at 6:33

Magento 2 creates the factory classes in the var/generation folder and if you add a new parameter in your __construct() and it's Factory class does not exists then Magento 2 throws the above error message. Even though there are few solutions give already for you question, i can suggest you another solution which does not require you to clear your generation folder. Please use the below command after any change is made related to Factory classes:

bin/magento setup:di:compile

This command can be more useful on production server.


It's just <magento-root>/var/ folder permission issue.

Execute below command using CLI mode

sudo chmod -R 777 var/

Class ...Factory does not exist means your model class not loaded. As Magento 2 based on modular programming so we need to specify the model class where we want to use explicitly. We can use our model class in two types:

1.by "use"

use Magento\Customer\Setup\CustomerSetupFactory;

public function __construct(CustomerSetupFactory $customerSetupFactory)
    $this->customerSetupFactory = $customerSetupFactory;
  1. Directly

    public function __construct(Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory){
    $this->customerSetupFactory = $customerSetupFactory;

When you hit the URL(http://domain.com/your module frontname) of your plugin on browser than the Magento generate the code for your module as below:

\var\generation\Your Vendor Directory\Your Module Directory\Controller
\var\generation\Your Vendor Directory\Your Module Directory\Model

In your case you need to check the "CustomerFactory" and "OrderFactory" is used as above format or not.


Give 777 permission and change ownership to www-data of generated/code folder and then run the command sudo php bin/magento setup:di:compile clear cache and boom your problem is solved

  • setting permissions to 777 is a security vulverability
    – Cypher909
    Commented Jun 30, 2020 at 20:29

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.