How to clear PHP artisan cache for a single entry instead of everything

Viewed 907

Usually when I want to clear app cache I run php artisan cache:clear but that removes all cache entries. What I'm hoping to do is forget a single element from command line by specifying the key. For example, something like php artisan cache:clear --key=userids.

I looked through some documentation but couldn't find anything. Is this possible and if so how?

2 Answers

There is no php artisan cache:clear --key=userids command that I am aware of but you can delete a particular cache using tinker. Just run

php artisan tinker
Psy Shell v0.10.8 (PHP 8.0.6 — cli) by Justin Hileman
>>> Cache::get('me');
=> "I am Sandeep"
>>> Cache::forget('me');
=> true
>>> Cache::get('me');
=> null

Hop into the Laravel shell via php artisan tinker, and then type/paste Cache::forget('userids'); in there.

If you find yourself needing to do this frequently, consider a custom Artisan command.

Related