160

The question is pretty clear.

php artisan cache:clear

Is there any workaround to clear the cache like the above command but without using CLI. I am using a popular shared hosting service, but as per my plan, I don't have control panel access.

I want to clear the views cache.

I saw a question almost the same like this, but it doesn't help me.

STA
  • 30,729
  • 8
  • 45
  • 59
Duke
  • 35,420
  • 13
  • 53
  • 70
  • 2
    Running Laravel on shared hosting is insane, IMO, for precisely this sort of reason. How are you running migrations? – ceejayoz Jul 16 '15 at 15:01
  • @ceejayoz .. Actually I just started this project, and its my first laravel project as well. I didn't come to this migration thing yet.. – Duke Jul 19 '15 at 07:58
  • 14
    "Running Laravel on shared hosting is insane" @ceejayoz ... But this is the real world. Sometimes you have to because there's no choice. – elb98rm Sep 01 '16 at 08:47
  • and this worked in laravel 8 – Azade Dec 01 '21 at 15:31

20 Answers20

185

You can call an Artisan command outside the CLI.

Route::get('/clear-cache', function() {
    $exitCode = Artisan::call('cache:clear');
    // return what you want
});

You can check the official doc here http://laravel.com/docs/5.0/artisan#calling-commands-outside-of-cli


Update

There is no way to delete the view cache. Neither php artisan cache:cleardoes that.

If you really want to clear the view cache, I think you have to write your own artisan command and call it as I said before, or entirely skip the artisan path and clear the view cache in some class that you call from a controller or a route.

But, my real question is do you really need to clear the view cache? In a project I'm working on now, I have almost 100 cached views and they weight less then 1 Mb, while my vendor directory is > 40 Mb. I don't think view cache is a real bottleneck in disk usage and never had a real need to clear it.

As for the application cache, it is stored in the storage/framework/cache directory, but only if you configured the file driver in config/cache.php. You can choose many different drivers, such as Redis or Memcached, to improve performances over a file-based cache.

Marco Pallante
  • 3,923
  • 1
  • 21
  • 26
  • 1
    What *cache* are you trying to clear? The `cache:clear` command works with the *application cache*, the one that is accessed from the `Cache` facade. – Marco Pallante Jul 16 '15 at 15:19
  • I want to clear the view cache, there are a lot of pages saved in view cache folder. One more question, when says application cache, which are all the directories it targets. – Duke Jul 19 '15 at 04:47
  • Thanks for the explanation!. I am not worried bout disks space :) But one question if view cache is not clearing, then how the new changes in view get affect in the website, is that any I/O check happening in laravel ? – Duke Jul 19 '15 at 06:48
  • 1
    I think it does a timestamp checking on files, which is faster than rebuilding the blade template – Marco Pallante Jul 19 '15 at 08:02
  • Yea, I guess so, anyway you helped me to think in the right way, thanks! – Duke Jul 19 '15 at 12:36
  • You need to clear view cache if you make a custom directive, which was my case. – Phiter Nov 27 '17 at 19:51
  • 1
    "There is no way to delete the view cache" This is simply not true. You can use `php artisan view:clear` to do just that. – jfadich Nov 07 '18 at 21:42
84

Go to laravelFolder/bootstrap/cache then rename config.php to anything you want eg. config.php_old and reload your site. That should work like voodoo.

Dharman
  • 30,962
  • 25
  • 85
  • 135
The Billionaire Guy
  • 3,382
  • 28
  • 31
  • 1
    I confirm. Works in laravel 5.4. Me bows. – simon May 31 '17 at 01:14
  • 1
    Confirmed it wipe the pain in the a** while hosting laravel 5.4 from windows to shared hosting.... THANK YOU!!! – Rikudo Pain Jul 03 '17 at 17:52
  • 1
    It seems that in a few scenarios, this is the only solution that works. If you have a cached config, and then you remove a Facade or Service Provider, when you run the command to create the new cached config, it runs using the existing cached config, and tries to reference the Facade and/or Service Provider classes that no longer exist and fails. The other option would be to remove the references from the config file, regenerate your cached config, then remove the actual Facade and/or Service Provider classes. – Jason Wheeler Sep 28 '17 at 02:08
  • 2
    For Laravel 6.6, To remove caching, I just removed `cache` folder & in `.env` file default cache value. But after deleting all this getting problem again.. when `php artisan optimize`. Finally this one solved my problem, not by renaming but changing the key value in config.php file. Thanks @DeadGuy – Chandan Sharma Dec 18 '19 at 10:48
  • 1
    Confirming that it still works for Laravel 9. Thanks a bunch! – Imtiaz Oct 13 '22 at 21:26
  • 1
    Working with latest Laravel 5.4.22 – aswzen Dec 05 '22 at 04:33
63

As I can see: http://itsolutionstuff.com/post/laravel-5-clear-cache-from-route-view-config-and-all-cache-data-from-applicationexample.html

is it possible to use the code below with the new clear cache commands:

//Clear Cache facade value:
Route::get('/clear-cache', function() {
    $exitCode = Artisan::call('cache:clear');
    return '<h1>Cache facade value cleared</h1>';
});

//Reoptimized class loader:
Route::get('/optimize', function() {
    $exitCode = Artisan::call('optimize');
    return '<h1>Reoptimized class loader</h1>';
});

//Route cache:
Route::get('/route-cache', function() {
    $exitCode = Artisan::call('route:cache');
    return '<h1>Routes cached</h1>';
});

//Clear Route cache:
Route::get('/route-clear', function() {
    $exitCode = Artisan::call('route:clear');
    return '<h1>Route cache cleared</h1>';
});

//Clear View cache:
Route::get('/view-clear', function() {
    $exitCode = Artisan::call('view:clear');
    return '<h1>View cache cleared</h1>';
});

//Clear Config cache:
Route::get('/config-cache', function() {
    $exitCode = Artisan::call('config:cache');
    return '<h1>Clear Config cleared</h1>';
});

It's not necessary to give the possibility to clear the caches to everyone, especially in a production enviroment, so I suggest to comment that routes and, when it's needed, to de-comment the code and run the routes.

Francesco
  • 2,042
  • 2
  • 19
  • 29
50

Config caching The laravel config spreads across dozens of files, and including every one of them for each request is a costly process. To combine all of your config files into one, use:

php artisan config:cache

Keep in mind that any changes to the config will not have any effect once you cache it. To refresh the config cache, run the above command again. In case you want to completely get rid of the config cache, run

php artisan config:clear

Routes caching Routing is also an expensive task in laravel. To cache the routes.php file run the below command:

php artisan route:cache

Mind that it doesn't work with closures. In case you're using closures this is a great chance to move them into a controller, as the artisan command will throw an exception when trying to compile routes that are bound to closures instead of proper controller methods. In the same as the config cache, any changes to routes.php will not have any effect anymore. To refresh the cache, run the above command everytime you do a change to the routes file. To completely get rid of the route cache, run the below command:

php artisan route:clear

Classmap optimization

It's not uncommon for a medium-sized project to be spread across hundreds of PHP files. As good coding behaviours dictate us, everything has its own file. This, of course, does not come without drawbacks. Laravel has to include dozens of different files for each request, which is a costly thing to do.

Hence, a good optimization method is declaring which files are used for every request (this is, for example, all your service providers, middlewares and a few more) and combining them in only one file, which will be afterwards loaded for each request. This not different from combining all your javascript files into one, so the browser will have to make fewer requests to the server.

The additional compiles files (again: service providers, middlewares and so on) should be declared by you in config/compile.php, in the files key. Once you put there everything essential for every request made to your app, concatenate them in one file with:

php artisan optimize --force

Optimizing the composer autoload

This one is not only for laravel, but for any application that's making use of composer.

I'll explain first how the PSR-4 autoload works, and then I'll show you what command you should run to optimize it. If you're not interested in knowing how composer works, I recommend you jumping directly to the console command.

When you ask composer for the App\Controllers\AuthController class, it first searches for a direct association in the classmap. The classmap is an array with 1-to-1 associations of classes and files. Since, of course, you did not manually add the Login class and its associated file to the classmap, composer will move on and search in the namespaces. Because App is a PSR-4 namespace, which comes by default with Laravel and it's associated to the app/ folder, composer will try converting the PSR-4 class name to a filename with basic string manipulation procedures. In the end, it guesses that App\Controllers\AuthController must be located in an AuthController.php file, which is in a Controllers/ folder that should luckily be in the namespace folder, which is app/.

All this hard work only to get that the App\Controllers\AuthController class exists in the app/Controllers/AuthController.php file. In order to have composer scanning your entire application and create direct 1-to-1 associations of classes and files, run the following command:

composer dumpautoload -o

Keep in mind that if you already ran php artisan optimize --force, you don't have to run this one anymore. Since the optimize command already tells composer to create an optimized autoload.

Al.G.
  • 4,327
  • 6
  • 31
  • 56
Maulik
  • 877
  • 1
  • 8
  • 17
46

This package is for php ^7.0 and ^laravel5.5.

Use this package in cronjob that I have created for this purpose only. I was also facing same situation. https://packagist.org/packages/afrazahmad/clear-cached-data Install it and run:

php artisan clear:data

and it will run the following commands automcatically

php artisan cache:clear
php artisan view:clear
php artisan route:clear
php artisan clear-compiled
php artisan config:cache

Hope it helps.

If you want to run it automatically at specific time then you will have to setup cronjob first. e.g.

 in app/console/kernel.php

In schedule function:

$schedule->command('clear:data')->dailyAt('07:00');
Afraz Ahmad
  • 5,193
  • 28
  • 38
17

Basically I want to clear the views cache.

There is now a command in Laravel 5.1 for that

php artisan view:clear
Laurence
  • 58,936
  • 21
  • 171
  • 212
15

To clear all cache outside CLI, Do this; This works for me.

Route::get('/clear', function() {

   Artisan::call('cache:clear');
   Artisan::call('config:clear');
   Artisan::call('config:cache');
   Artisan::call('view:clear');

   return "Cleared!";

});
Amos Chihi
  • 375
  • 3
  • 7
13

This command will clear all kinds of cache at once. :

$ php artisan optimize:clear

This is an alias of :

$ php artisan view:clear
$ php artisan config:clear
$ php artisan route:clear
$ php artisan cache:clear
$ php artisan clear-compiled

This method added on Laravel 5.7

STA
  • 30,729
  • 8
  • 45
  • 59
11

You can connect via FTP and clear storage\framework\views folder for laravel 5 or app\storage\views for laravel 4.

Christian Giupponi
  • 7,408
  • 11
  • 68
  • 113
gandra404
  • 5,727
  • 10
  • 52
  • 76
4
php artisan view:clear

will clear the cached views

Spidi
  • 109
  • 5
  • 13
  • 1
    The alternative to the command ```php artisan cache:clear ```is asked. This wont clear viewed cache stored in the storage folder, which is exactly what is needed. – Spidi Oct 10 '18 at 09:35
4

Local Machine

Run php artisan config:cache in terminal of your project root directory.

On Hosting Server

First try to get access of terminal on from hosting provider. Then run php artisan config:cache command in your project root directory.

If you don't have terminal access the follow this trick.

  1. go to this directory project-folder/bootstrap/cache
  2. rename or delete config.php file

Note: avoid to delete file it may create problems in future by renaming you can change file name so I suggest to rename file name.

msayubi76
  • 1,446
  • 1
  • 12
  • 18
  • This answer doesn't add any information that didn't get posted yet – Daniel W. Dec 27 '21 at 12:23
  • @DanielW. information about what? – msayubi76 Dec 28 '21 at 08:56
  • The artisan command has been posted in a different answer and also renaming or deleting the config.php. What does your answer add thats new? – Daniel W. Dec 28 '21 at 09:42
  • yeah you are right but this ans elobrate that what you can do in a maximum way on local server, shared server with limited access and dedicated server with fully access to solve your problem – msayubi76 Dec 28 '21 at 10:09
2

You can do this if you are using Lumen from Laravel on your routes/web.php file:

use Illuminate\Support\Facades\Artisan;

$app->get('/clear-cache', function () {
    $code = Artisan::call('cache:clear');
    return 'cache cleared';
});
pableiros
  • 14,932
  • 12
  • 99
  • 105
2

Used this page a few times to copy and paste quick commands into composer, so I wrote a command that does these commands in one single artisan command.

namespace App\Console\Commands\Admin;

use Illuminate\Console\Command;

class ClearEverything extends Command
{

    protected $signature = 'traqza:clear-everything';

    protected $description = 'Clears routes, config, cache, views, compiled, and caches config.';

    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        $validCommands = array('route:clear', 'config:clear', 'cache:clear', 'view:clear', 'clear-compiled', 'config:cache');
        foreach ($validCommands as $cmd) {
            $this->call('' . $cmd . '');

        }
    }
}

Place in app\Console\Commands\Admin folder

then run command in composer php artisan traqza:clear-everything

Happy coding.

Github -> https://github.com/Traqza/clear-everything

levi
  • 1,566
  • 3
  • 21
  • 37
1

You can do it via router as well, similar to Francesco answer but with less clutter in router config

Route::get('/artisan/{cmd}', function($cmd) {
    $cmd = trim(str_replace("-",":", $cmd));
    $validCommands = ['cache:clear', 'optimize', 'route:cache', 'route:clear', 'view:clear', 'config:cache'];
    if (in_array($cmd, $validCommands)) {
        Artisan::call($cmd);
        return "<h1>Ran Artisan command: {$cmd}</h1>";
    } else {
        return "<h1>Not valid Artisan command</h1>";
    }
});

Then run them via visiting http://myapp.test/artisan/cache-clear etc If you need to add/edit valid Artisan commands just update the $validCommands array.

chemic
  • 800
  • 7
  • 12
1

This worked for me. In your project go to: storage > framework > views. Delete all the files in there and refresh your page.

rogramatic
  • 141
  • 1
  • 5
1

To Clear Cache Delete all files in cache folder in your shared hosting

Laravel project->bootstarp->cache->delete all files
Balaji
  • 9,657
  • 5
  • 47
  • 47
1

You can clear laravel cache programmatically

use Illuminate\Support\Facades\Cache;

Cache::flush();
cache()->flush();
Faridul Khan
  • 1,741
  • 1
  • 16
  • 27
0

While I strongly disagree with the idea of running a laravel app on shared hosting (a bad idea all around), this package would likely solve your problem. It is a package that allows you to run some artisan commands from the web. It's far from perfect, but can work for some usecases.

https://github.com/recca0120/laravel-terminal

George
  • 115
  • 1
  • 3
0

Cache::flush(); https://laravel.com/docs/5.7/cache#events This work in the class Handler extends ExceptionHandler

0

I believe the more efficient approach to this is to use the cron job module in the shared server admin panel to run the laravel scheduler command which will in turn call the configured artisan command, something like this should do the job:

* * * * * /usr/bin/php /var/www/web/artisan schedule:run /dev/null 2>&1

With scheduler setup in cron, you can edit the schedule method in \App\Console\Kernel.php to call the right artisan command, something like this:

$schedule->command('queue:work')->cron('* * * * *')->withoutOverlapping();
$schedule->command('route:cache')->cron('0 0 * * *')->withoutOverlapping();

You can always delete the lines above after the commands run

korwalskiy
  • 927
  • 12
  • 12