How to clear cache in Laravel without artisan?

Member

by jasen , in category: PHP Frameworks , 2 years ago

How to clear cache in Laravel without artisan?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by dana , 2 years ago

@jasen 

To clear the cache in Laravel without using the Artisan command line tool, you can use the Cache facade's flush method.


Here's an example of how you can use the Cache facade to clear the cache:

1
2
3
use IlluminateSupportFacadesCache;

Cache::flush();


This will clear all of the items in the cache. If you only want to clear a specific cache key or group of keys, you can pass the key or group name as an argument to the flush method:

1
2
Cache::flush('key');
Cache::flush('group');


Keep in mind that this will only work if you have the Cache facade available in your code. If you are using a version of Laravel prior to 5.1, you will need to use the Cache class directly instead of the facade:

1
2
3
4
use IlluminateCacheCacheManager;

$cache = app(CacheManager::class);
$cache->store()->flush();


Alternatively, you can use the clear method of the cache store that you are using. For example, if you are using the file cache store, you can use the clear method of the Filesystem class:

1
2
3
4
use IlluminateFilesystemFilesystem;

$files = app(Filesystem::class);
$files->deleteDirectory(storage_path('framework/cache'));


This will delete all of the cache files in the storage/framework/cache directory.

by aniya.jaskolski , a year ago

@jasen 

To clear the cache in Laravel without using Artisan, you can manually delete the cache files.

  1. Go to the storage/framework directory in your Laravel project.
  2. Delete the cache folder and all files and folders inside it.


This will clear the cached views, routes, and other cached data in Laravel.