Magento 2 var/generation cannot be deleted warning
You may have noticed some problems when setting up a new Magento 2 shop. A typical warning you may get is: var/generation cannot be deleted. I give you advice on how to react on this properly.
Magento 2 var/generation cannot be deleted warning
If your shop is in production mode and you add a new module or change some injections of your own module you have to compile it. For this you may use a terminal command like this:
1 | php bin/magento setup:di:compile |
In development mode everything works fine, but on your production system you may get an error message like this:
1 2 3 4 5 6 7 8 9 | The directory "/path_to_project/var/generation" cannot be deleted Warning!rmdir(/path_to_project/var/generation): Directory not empty#0 /path_to_project/setup/src/Magento/Setup/Console/CompilerPreparation.php(68): Magento\Framework\Filesystem\Driver\File->deleteDirectory('/path_to_project/...') #1 /path_to_project/vendor/magento/framework/Console/Cli.php(74): Magento\Setup\Console\CompilerPreparation->handleCompilerEnvironment() #2 /path_to_project/bin/magento(22): Magento\Framework\Console\Cli->__construct('Magento CLI') #3 {main} PHP Fatal error: Uncaught Error: Class 'Cli' not found in /path_to_project/bin/magento:31 Stack trace: #0 {main} thrown in /path_to_project/bin/magento on line 31 |
The problem
For an inexperienced developer this looks like a file permission problem on your host. If you now try to change file permissions to try to completely delete var/generation folder you may create an error on your store front or to open security risks. The simple but effective way to solve this problem is to do a simple static deploy before you run compilation.
Solution
The solution is this command:
1 | php bin/magento setup:static-content:deploy |
After a quick static-content deploy you can compile your modules without a problem. If your store is not in language English your may have to declare a language for static deploy like:
1 | php bin/magento setup:static-content:deploy en_US de_DE |
For our German speaking shop we had to set en_US and de_DE in that order, otherwise lots of strings are in English on frontend and backend.
Conclusion
If you get a var/generation cannot be deleted Warning message for compile on a shop in production mode I showed you a simple but effective way to solve this without deleting files or to change file permissions.
Hi,
I ‘ve tried to run the magento command: php bin/magento, got the following errors:
“We’re sorry, an error occurred. Try clearing the cache and code generation directories. By default, they are: var/cache, var/di, var/generation, and var/page_cache.
[Exception]
Deprecated Functionality: The each() function is deprecated. This message will be suppressed on further calls in /var/www/ html/vendor/magento/zendframework1/library/Zend/Cache/Backend.php on line 79”
Even I’ve already flushed the caches, but does not solve the issue. I cannot run any magento command. Any help would be appreciated. Thanks
Can you check your PHP version? Is it possible that you use 7.2? https://laravel-news.com/php-7-2-deprecations
$ php bin/magento setup:static-content:deploy
PHP Fatal error: Uncaught Error: Class ‘DOMDocument’ not found in /home/ubuntu/dnmp/www/magento/vendor/magento/framework/Config/Dom.php:384
Stack trace:
$ php –version
PHP 7.0.22-0ubuntu0.16.04.1 (cli) ( NTS )
Perfect thanks so much. Im so ready to be done with this magento crap. Glad this was an easy one
Thank you for your comment. It’s good to read, that my posts are helpful.
Thank you so much! This solution is work for me.