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?

  • You have to make script in root of your project. – Rakesh Jesadiya Dec 15 '16 at 11:21
  • Actually I want to delete it just after controller action task. – Arshad Hussain Dec 15 '16 at 11:23
  • You don't need any controller to do a simple task. You can take advantage of the symphony console tools to create your own commands. Check out the answer I've posted. – André Ferraz Dec 15 '16 at 16:29

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 under app/code/Andre/, include the registration.php and module.xml.




<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Andre_Tools" setup_version="0.1.0"/>

Create a new model clas, this is where it will contains the options, description and the logic of your command.


namespace Andre\Tools\Model;

use \Symfony\Component\Console\Command\Command;
use \Symfony\Component\Console\Input\InputInterface;
use \Symfony\Component\Console\Output\OutputInterface;

class Generation extends Command
    protected function configure()
             ->setDescription('The description of you command here!');


    protected function execute(InputInterface $input, OutputInterface $output)
        $output->writeln('Hello World!');


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\Console\CommandList">
            <argument name="commands" xsi:type="array">
                <item name="clean" xsi:type="object">Andre\Tools\Model\Generation</item>

Lastly do a bin/magento setup:upgrade, check that the module is active bin/magento module:status if not then run bin/magento module:enable Andre_Tools.

Now to run the command you just create simply run:

 bin/magento generation:clean

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.


below is the code which you can run inside the PHP..

<?php system("rm -rf RESOURCE_PATH"); ?>

with above code you can run most of the CLI commands.

some system don't support that so you can use below to method to delete the folder.

function remove($resourcePath){
if (is_dir($resourcePath) === true)
    $datas = array_diff(scandir($resourcePath), array('.', '..'));

    foreach ($datas as $data)
        remove(realpath($resourcePath) . '/' . $resourcePath);

    return rmdir($resourcePath);

else if (is_file($resourcePath) === true)
    return unlink($resourcePath);

return false;}

Hope the solution will work for you.. the first code I did not tested in windows environment but it should work there also.

you can put either of the above code anywhere into you module(but make sure you are giving the correct path), can run on event/crone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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