E:\xampp\htdocs\magento-2>php -dmemory_limit=5G bin/magento setup:di:compile The directory "E:/xampp/htdocs/magento-2/var/generation/Magento/Framework" canno t be deleted Warning!rmdir(E:/xampp/htdocs/magento-2/var/generation/Magento/Fram ework): Directory not empty#0 E:\xampp\htdocs\magento-2\vendor\magento\framework \Filesystem\Driver\File.php(403): Magento\Framework\Filesystem\Driver\File->dele teDirectory('E:/xampp/htdocs...')

  1. E:\xampp\htdocs\magento-2\vendor\magento\framework\Filesystem\Driver\File.php (403): Magento\Framework\Filesystem\Driver\File->deleteDirectory('E:/xampp/htdoc s...')
  2. E:\xampp\htdocs\magento-2\setup\src\Magento\Setup\Console\CompilerPreparation .php(68): Magento\Framework\Filesystem\Driver\File->deleteDirectory('E:/xampp/ht docs...')
  3. E:\xampp\htdocs\magento-2\vendor\magento\framework\Console\Cli.php(74): Magen to\Setup\Console\CompilerPreparation->handleCompilerEnvironment()
  4. E:\xampp\htdocs\magento-2\bin\magento(22): Magento\Framework\Console\Cli->__c onstruct('Magento CLI')
  5. {main} PHP Fatal error: Class 'Cli' not found in E:\xampp\htdocs\magento-2\bin\magento on line 31

Fatal error: Class 'Cli' not found in E:\xampp\htdocs\magento-2\bin\magento on l ine 31


2 Answers 2


Removed your var/generation directory and var/cache directory.

You need to remove it manually, if it is ubuntu then locate in your magento 2 root directory and run rm -rf var/generation and rm -rf var/cache command.

Then try to run below commands in sequence.

  • php bin/magento setup:di:compile
  • php bin/magento setup:upgrade
  • php bin/magento cache:clean
  • php bin/magento cache:flush
  • mine is a windows system
    – learner
    Commented Jun 23, 2017 at 9:59
  • @Priya then remove it manually. Commented Jun 23, 2017 at 11:05
  • @ManthanDave In windows wamp i removed generation, still showing error like : The directory "C:/wamp64/www/magento2/generated/code/Magento/Framework/App" ca ot be deleted Warning!rmdir(C:/wamp64/www/magento2/generated/code/Magento/Fram ork/App): Directory not empty How to solve the error?
    – zus
    Commented Feb 21, 2020 at 11:57
  • @ManthanDave Screenshot : snipboard.io/FYa9EU.jpg
    – zus
    Commented Feb 21, 2020 at 12:00
  • 3
    I think, one should run the setup:upgrade command first and then the setup:di:compile Commented Sep 29, 2020 at 10:41

If production mode, put site into maintenance mode first.

bin/magento maintenance:enable
bin/magento setup:upgrade
bin/magento setup:di:compile
bin/magento setup:static-content:deploy
bin/magento cache:flush
// If running as root instead of www-data for example you may need to chown/chmod.
// chown -R www-data:www-data .

bin/magento maintenance:disable

Maybe it is because stuff is being generated as bots/users try to access the site?

Note: If you use other locales you would want to setup:static-content:deploy those as well.

I think you can specify multiple after the statup:static-content:deploy en_US en_GB en_NZ etc...

See more here: https://devdocs.magento.com/guides/v2.4/install-gde/install/cli/install-cli-subcommands-maint.html

You might need to take ownership of the folder first or shell in as the Magento filesystem owner.

  • 5
    This is definitely the right answer; if you don't enable maintenance mode first you will get spammed with The directory "/var/www/html/generated/code/Magento" cannot be deleted Warning!rmdir(/var/www/html/generated/code/Magento): Directory not empty for many directories. Enabling maintenance mode first and waiting a bit for in-flight requests to finish really is the solution here
    – Justin
    Commented Oct 24, 2020 at 14:28
  • This might be the solution since it's possible some users are actively accessing the site and prevents the command to complete due to the directories being created on the fly.
    – Eduardo B.
    Commented Dec 9, 2021 at 0:51

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.