In the fast-paced world of web development, efficiency and performance are paramount. Laravel, a powerful PHP framework, provides an elegant syntax and tools that make it a preferred choice for many developers. One of the essential aspects of maintaining a Laravel application’s performance is effective cache management. Caching can significantly reduce load times by storing a copy of the page generated by the PHP code, making subsequent requests faster as the server doesn’t need to reprocess everything. However, to ensure that users receive the most up-to-date content, it’s crucial to know how to clear the cache properly.
This guide will explore efficient strategies for clearing cache in Laravel, ensuring your applications run smoothly and efficiently.
Understanding Laravel’s Caching Mechanisms
Before diving into the strategies for clearing cache, it’s essential to understand the various caching mechanisms Laravel supports:
- Route Cache: Laravel caches the application’s routes to speed up route registration and improve performance.
- Config Cache: Configuration files are cached to reduce the overhead of loading and parsing these files on each request.
- View Cache: Compiled views are cached to avoid recompiling the blade templates on every request.
- Application Cache: Laravel allows caching of data such as query results, objects, or arrays to speed up application performance.
Clearing Laravel Cache Using PHP Artisan
Log in to the system running your Laravel application and open a terminal. Then navigate to your Laravel application code. Here you can issue the commands to clear the cache as follows:
- Clear All Cache
To clear the cache in Laravel using the PHP Artisan command-line interface, you can use the
cache:clear
command. This command removes all items from the cache, regardless of the cache driver being used.- Here’s an example of how to use the
cache:clear
command to clear the Laravel cache:php artisan cache:clear
When you run the
cache:clear
command, you should see a message indicating that the cache was successfully cleared. - If the Laravel application uses multiple caches, then you can also specify the caching store to clear. For example to clear all cache stored from the Redis server, type:
php artisan cache:clear --store redis
- You can also clear the specific items from the cache by defining the tags with the command:
php artisan cache:clear --tags=tag1,tag2
- Here’s an example of how to use the
- Clear Portion of Cache
Note that the
cache:clear
command is an administrative command, and it is not recommended for use in production environments. If you want to clear only a specific portion of the cache, you can use thecache:forget
command to delete specific keys instead.To use the
cache:forget
command, you can specify the key as an argument. For example:php artisan cache:forget key
- Clear Route Cache
To clear the route cache of your Laravel application execute the following command from the shell.
php artisan route:clear
- Clear Configuration Cache
You can use
config:clear
to clear the config cache of the Laravel application.php artisan config:clear
- Clear Compiled Views Cache
Also, you may need to clear compiled view files of your Laravel application. To clear compiled view files run the following command from the terminal.
php artisan view:clear
Clear Laravel Cache with PHP Programing
To clear the cache in Laravel, you can use the Cache facade
or the cache helper function. These provide a unified interface for interacting with the various cache drivers, allowing you to store and retrieve data in a consistent manner regardless of the underlying storage mechanism.
Here’s a step-by-step guide on how to clear the cache in Laravel:
- 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. This method removes all keys from the cache, regardless of the cache driver being used.Cache::flush();
- Use the
Cache::forget
method to delete a specific key from the cache. This method takes a key as an argument and removes the corresponding key-value pair from the cache.Cache::forget('key');
That’s it! The cache is now cleared, and all keys have been removed.
Note that the Cache::flush
method is an administrative command, and it is not recommended for use in production environments. If you want to clear only a specific portion of the cache, you can use the Cache::forget method to delete specific keys instead.
Conclusion
Effective cache management is a critical aspect of developing high-performing Laravel applications. By utilizing Laravel’s caching mechanisms and understanding when and how to clear the cache, developers can ensure that their applications remain efficient, responsive, and up-to-date. Remember, caching is a powerful tool when used wisely, but it requires careful management to avoid serving stale content or creating performance bottlenecks.
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.