How to remove var/generation
folder programmatically.
I have used below via command line:
sudo rm -rf var/cache var/generation var/di
But I want to do it programmatically. Is it possible?
magento2setup-di-compile
How to remove var/generation
folder programmatically.
I have used below via command line:
sudo rm -rf var/cache var/generation var/di
But I want to do it programmatically. Is it possible?
Best Answer
I've created several useful commands, which are useful for us who develop Magento 2 sites on a daily basis. Magento 2 console commands are based on symphony, you can create commands for your personal/team use, something like
bin/magento cache:clean
. This way you can execute the command directly from the terminal.Here is a simple hello world command. Before we get started clear you generation folder and here is what you need to do.
Create a new module for illustration purposes I'll call it
Tools
underapp/code/Andre/
, include theregistration.php
andmodule.xml
.app/code/Andre/Tools/registration.php
app/code/Andre/Tools/etc/module.xml
Create a new model clas, this is where it will contains the options, description and the logic of your command.
app/code/Andre/Tools/Model/Generation.php
app/code/Andre/Tools/etc/di.xml
Lastly do a
bin/magento setup:upgrade
, check that the module is activebin/magento module:status
if not then runbin/magento module:enable Andre_Tools
.Now to run the command you just create simply run:
Now just add your logic under the
execute()
method to delete the generation folder. It shouldn't be hard to delete a folder with PHP.