DEV Community

Cover image for Laravel - Clear Cache in Shared Hosting Server
Suresh Ramani
Suresh Ramani

Posted on β€’ Originally published at techvblogs.com

3 1 1

Laravel - Clear Cache in Shared Hosting Server

Shared Hosting is very wired Hosting I have ever used. Because All things are restricted which is required for Laravel Project. You cannot install composer using the command line so you need to upload the whole vendor folder from your local machine. Also, there are many things you cannot do without the support of a Hosting Provider. There are so many restrictions to using Shared Hosting that's why we use Self Managed Cloud Hosting using ServerAvatar.

If you want to clear cache in Laravel which is hosted on Shared Hosting then this article will help you to clear cache in a shared hosting server.

Laravel - Clear Cache in Shared Hosting Server

There are different methods to clear cache in Shared Hosting Server.

1. Laravel - Clear Cache in Shared Hosting Server using Web Routes

 //Clear route cache
 Route::get('/route-cache', function() {
     \Artisan::call('route:cache');
     return 'Routes cache cleared';
 });

 //Clear config cache
 Route::get('/config-cache', function() {
     \Artisan::call('config:cache');
     return 'Config cache cleared';
 }); 

 // Clear application cache
 Route::get('/clear-cache', function() {
     \Artisan::call('cache:clear');
     return 'Application cache cleared';
 });

 // Clear view cache
 Route::get('/view-clear', function() {
     \Artisan::call('view:clear');
     return 'View cache cleared';
 });

 // Clear cache using reoptimized class
 Route::get('/optimize-clear', function() {
     \Artisan::call('optimize:clear');
     return 'View cache cleared';
 });
Enter fullscreen mode Exit fullscreen mode

2. Laravel - Clear Cache in Shared Hosting Server using Task Scheduling (Cronjob)

If you want to run it automatically at a specific time then you will have to set up cronjob first.

Open app\Console\Kernel.php and update the following code:

protected function schedule(Schedule $schedule)
{
    $schedule->command('cache:clear')->hourly();
    $schedule->command('config:clear')->hourly();
    $schedule->command('view:clear')->hourly();
    $schedule->command('route:clear')->hourly();
    $schedule->command('optimize:clear')->hourly();
}
Enter fullscreen mode Exit fullscreen mode

You can set any cronjob schedule. You can refer to this document for the task scheduling in laravel. Also, You need to refer to the documentation of the Hosting Provider to Set Cronjob.

For cPanel, You can refer to this article to set Cronjob in cPanel.

Thank you for reading this article.

Neon image

Resources for building AI applications with Neon Postgres πŸ€–

Core concepts, starter applications, framework integrations, and deployment guides. Use these resources to build applications like RAG chatbots, semantic search engines, or custom AI tools.

Explore AI Tools β†’

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments. Some comments have been hidden by the post's author - find out more

PulumiUP 2025 image

PulumiUP 2025: Cloud Innovation Starts Here

Get inspired by experts at PulumiUP. Discover the latest in platform engineering, IaC, and DevOps. Keynote, demos, panel, and Q&A with Pulumi engineers.

Register Now

Pieces AI Productivity Summit

​Join top AI leaders, devs, & enthusiasts for expert talks, live demos, and panels on how AI is reshaping developer productivity at the Pieces AI Productivity Summit.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❀️