In the fast world of making websites, being quick and efficient is very important. Laravel is a popular PHP framework because it has easy-to-understand tools and syntax. One important part of keeping a Laravel app running well is managing the cache. Caching helps make pages load faster by storing a copy of the page, so the server doesn’t have to process everything again for each request. But to make sure users get the latest content, it’s important to know how to clear the cache properly.
This guide will show you how to clear cache in Laravel easily, ensuring your apps run smoothly and quickly.
Laravel’s Caching Mechanisms
Before we get into how to clear the cache, let’s understand the different types of caching in Laravel:
- Route Cache: Caches the app’s routes to make route registration faster.
- Config Cache: Caches configuration files to reduce the time needed to load these files on each request.
- View Cache: Caches compiled views to avoid recompiling templates every time.
- Application Cache: Caches data like query results, objects, or arrays to make the app faster.
Clearing Laravel Cache Using PHP Artisan
Log in to the system where your Laravel app is running and open a terminal. Go to your Laravel app’s code folder. You can use the following commands to clear the cache:
- Clear All Cache
To clear all cache in Laravel, use the
cache:clear
command. This removes all items from the cache.- Example command:
php artisan cache:clear
You will see a message saying the cache was successfully cleared.
- If your app uses multiple caches, you can specify which one to clear. For example, to clear cache stored on a Redis server, use:
php artisan cache:clear --store redis
- You can also clear specific items from the cache by using tags:
php artisan cache:clear --tags=tag1,tag2
- Example command:
- Clear Portion of Cache
Using
cache:clear
is not recommended in production. To clear specific parts of the cache, usecache:forget
to delete specific keys.
Example command:php artisan cache:forget key
- Clear Route Cache
To clear the route cache, use:
php artisan route:clear
- Clear Configuration Cache
To clear the config cache, use:
php artisan config:clear
- Clear Compiled Views Cache
To clear compiled view files, use:
php artisan view:clear
Clear Laravel Cache with PHP Programming
You can also clear the cache using PHP code. The Cache facade
or cache helper function lets you interact with various cache drivers consistently.
Here’s how to do it:
- Import the Cache facade at the top of your PHP script:
use Illuminate\Support\Facades\Cache;
- Use the
Cache::flush
method to clear the entire cache:Cache::flush();
- Use the
Cache::forget
method to delete a specific key from the cache:Cache::forget('key');
That’s it! The cache is now cleared, and all keys have been removed.
Note: The Cache::flush
method is an administrative command and not recommended in production. Use Cache::forget
to delete specific keys instead.
Conclusion
Managing cache is crucial for high-performing Laravel apps. By using Laravel’s caching mechanisms and knowing how to clear the cache, developers can keep their apps efficient, responsive, and up-to-date. Caching is powerful but requires careful management to avoid serving old content or causing performance issues.
22 Comments
Thank for helpful!
Many Thanks …
Thanks you 🙂 Be safe
Thanks a lot. It’s very useful for me.
The question is pretty clear.
php artisan cache:clear
Is there any workaround to clear the cache like above we using in 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.**
All in one
Route::get(‘/clear-cache’, function() {
Artisan::call(‘optimize:clear’);
echo Artisan::output();
});
Thank you very much. It helps me a lot as a beginner in laravel.
Thanks buddy. Are you a Laravel developer?
Please change clear route cache from ‘php artisan route:cache ‘ to ‘php artisan route:clear’.
Thanks,
Anu
Thanks Anupama, I have corrected the command.
Thanks useful
i readed your infomation and I am very impressed for you. Thanks you for your sharing. It is very useful !
I have the same problem.
Calling / clear-cache is enough for all clients to see the same page, or do we have to make the call on each client computer?
Please fix this error in your post:
php artisan config:cache does not clear your cache. This will cause problems with unit tests.
It should be php artisan config:clear
thankss for sharing
In the browser:
instead of Artisan simply use
cache()->clear();
IMPORTANT! #3 should be:
php artisan config:clear
NOT config:cache
Hey, Rahul, would you mind updating the article? I spent a bunch of time debugging a problem with env() after running config:cache. Your article has a high Google result for “laravel clear cache” and it would be nice for the information to be correct.
php artisan route:cache ?? clear
I ran the commands and the laravel site stop working: appear 404
Not Found. Any help will be appreciate.
Thanks
php artisan route:cache is to cache the route
Run
php artisan route:clear
php artisan config:clear
Hello Jhon Corner I’m having same problem did you find a solution, I relly need the help.
Laravel provides an expressive, unified API for various caching backends. Also, Laravel is configured to use the file cache driver, which stores the serialized, cached objects in the filesystem. I would like to add that the above-listed steps to clear cache are easy to follow and important too for Laravel Application Development.