How to Clear Laravel Cache Without a Terminal

How to Clear Laravel Cache Without a Terminal

When building a website using Laravel, you might need to clear the cache, which is usually done with the command php artisan cache:clear. But what if you are using shared hosting and do not have a Terminal feature?

If that is your situation, you can clear the Laravel cache without a Terminal using several methods in this article.

How to Clear Laravel Cache Without a Terminal

To do this, you can use several methods ranging from creating a new route, using cron jobs, to manually deleting cache files from the file manager or FTP.

1. Clear Laravel Cache Using a Route

This method can be considered easy once you have successfully created a route, because later you only need to access a specific page on your website to clear the cache, for example by accessing https://staging.wpgan.com/clear-cache

However, this method is not recommended if you are still new to code, routes, or the Laravel system.

To do this, you can add the following route:

//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>';});

With that code, you can access the page according to the cache that we will clear. For example, if your website is usually accessed with https://wpgan.com and you want to perform an optimize, then you can access https://staging.wpgan.com/optimize according to the route that has been created.

2. Clear Laravel Cache Using a Cron Job

With this cron job, there are actually two options, using a cron job to run php artisan, or using a cron job to access the route page that was created in the first option periodically.

But here we will use a cron job to directly run php artisan. Besides that, this only provides the list of commands, so for how to add a cron job in cPanel, you can follow this tutorial: How to Create a Cron Job in cPanel

If you are already on the cron job page, you can add one of the following commands and have it run every 1 minute, 1 day, or 1 month.

/usr/bin/php /home/usernamecpanel/public_html/ artisan cache:clear >> /dev/null 2>&1
  • make sure usernamecpanel is changed to your cPanel username
  • public_html is changed to the path of your Laravel installation folder that contains the file artisan
  • cache:clear change according to your needs, for example to run php artisan optimize, you can change it to optimize

3. Clear Laravel Cache by Deleting Cache Files

This method is the easiest way if you have difficulty adding a route or creating a new cron job.

To do this, you only need to open the folder bootstrap/cache inside the Laravel folder on your hosting, then delete the file config.php to clear the configuration cache.

Those are several ways you can try to clear the Laravel cache without using a Terminal or SSH on shared hosting.

Written by

Muhammad Khoirun Nasikin

I enjoy tinkering with WordPress, hosting, VPS, web development, and various small experiments related to websites. I have over 3 years of experience in the web hosting industry.

COMMENTS

Loading...