title: Programmatically clear cache on symfony 2
body: <p>I'm trying to empty the Symfony 2 cache through my application just as if I was doing <code>php app/console cache:clear</code>.</p>
  
  <p>I've tried many different solutions, such as:</p>
  
  <pre><code>//solution 1
  $process = new Process('php '.__DIR__.'/../../../../../app/console cache:clear');
  $process-&gt;setTimeout(3600);
  $process-&gt;run();
  
  //solution 2
  $input = new StringInput(null);
  $output = new NullConfigurationOutput();
  $command = new CacheClearCommand();
  $command-&gt;setContainer($container);
  $command-&gt;run($input, $output);
  
  //solution 3    
  $container-&gt;get('cache_clearer')-&gt;clear($container-&gt;getParameter('kernel.cache_dir'));
  </code></pre>
  
  <p>I've also tried to <code>rm -Rf</code> the cache folder.</p>
  
  <p>None of them is  fully working as i'm getting many errors. The following error is the one i'm getting on solution 2 (which seemed to be the best):</p>
  
  <blockquote>
    <p>Failed to start the session: already started by PHP ($_SESSION is set).</p>
  </blockquote>
  
  <p>Seems like Symfony is trying to rebuild the page or reboot the application during the render process. </p>
  
  <p>How could someone clear the cache via some php code?</p>
  
  <p>EDIT :
  I'm actually trying to regenerate the cache because of my routing system. I've made a bundle  which take a symfony 2 route and add the possibility to rewrite it for SEO purpose (example : i have a front_category route which point to /category, with this bundle i can rewrite it to category-seo-optimized.html).</p>
  
  <p>If i'm on a route which needs particular parameter, such as an ID, i need to make a special route for it in order to rewrite it. For example, for /category/3 i need to create a route which is named category_3 and.. in order to let symfony knows i've changed the routing system, i had to clear the cache.</p>