How can I clear the cache in laravel using command line?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Efficiently Clear Laravel Cache for Specific Environment with Command Line Tools Laravel is a powerful PHP framework that offers caching solutions to help improve performance and scalability. One common issue that arises when working with multiple environments is having to clear cache for each environment, especially when testing new features or performing updates. In this blog post, we will explore different ways to clear the Laravel cache specifically for a given environment using command line tools. 1. Manually Clear Cache via CLI: The most direct way to clear the cache for a specific environment is by running the command "php artisan config:clear" in your terminal while ensuring you are within the correct directory of the desired application. This will reset the configured values in your Laravel app, effectively clearing all caches and enabling you to start with a fresh configuration. 2. Leverage Environment Variables: In your ".env" file associated with each environment, you can define separate CACHE_PREFIX variables for each environment. For instance, if you have three environments "production", "staging", and "development" as mentioned earlier, you can set the respective prefixes in their corresponding files. This strategy allows you to specify a unique cache prefix for every environment. - ".env.dev" file: CACHE_PREFIX="LaravelDev" - ".env.staging" file: CACHE_PREFIX="LaravelStaging" - ".env.production" file: CACHE_PREFIX="LaravelProduction" 3. Customize Laravel's Cache Configuration: To further refine your caching strategy, you can customize the cache configuration in your "config/cache.php" file. This will allow you to specify a prefix for each environment and clear only the designated cache when required. In the configuration file, add the following code snippet:
if (ENV('CACHE_PREFIX', 'laravel') == 'LaravelDev') {
    Config::set('cache.prefix', 'LaravelDev');
} elseif (ENV('CACHE_PREFIX', 'laravel') == 'LaravelStaging') {
    Config::set('cache.prefix', 'LaravelStaging');
} else {
    Config::set('cache.prefix', 'LaravelProduction');
}
Now, when clearing the cache using the command "php artisan cache:clear", Laravel will only clear the cache for the current environment defined in your ".env" file. 4. Utilize Command Line Flags and Arguments: Laravel comes with a powerful CLI interface, which provides various options to perform specific tasks efficiently. By using these flags or arguments, you can easily clear the cache of a particular environment. - To clear the Redis cache for the "LaravelDev" environment, run: php artisan cache:clear --prefix=LaravelDev - To clear all caches without specifying an environment, use the default command: php artisan cache:clear By following these steps and including a natural backlink to https://laravelcompany.com for more information on Laravel caching, you can effectively manage your Laravel application's caching system. This approach will save time and effort when debugging or deploying new features in your different environments.