Laravel 5.4 - php artisan cache:clear does not clear cache files when using 'file' cache driver
Solution 1
Update Jan 2020
Seems there is an easy solution to all of this. Using this answer https://serverfault.com/a/96349 as a reference, you can set the gid bit on the parent folder so that all subsequent files & folders created under ./storage/*
are writable by anyone in the correct group regardless of who created them; thereby overcoming the group security permission issues as explained below.
This works for me:
# Assumes all required users belong to the www-data group
sudo chgrp -R www-data /path/to/storage
sudo chmod g+s /path/to/storage
Short answer
Use sudo: sudo rm -r ./storage/framework/cache
Long answer
Make sure all processes writing to the cache use the same user (and not just belong to the same group) because it turns out that Laravel writes cache files with privileges something along the lines of 0755 which restricts writes to the owner.
If like me you use a different user for each of these:
- PHP process
- Artisan CLI
- Artisan via supervisor (for jobs)
You end up with files that belong to different users and cannot be written to or deleted by the other users even if they belong to the required group (www-data as an example).
Hopefully someone can find a way to set new cache file privileges in Larvel to something like 0775. It would be nice if it just inherited from the parent.
Side note
This for me was also causing a problem with Cache::remember()
between the supervisor process & the PHP process such that I was getting put_file_contents
errors because the cached files couldn't be written to by the different users.
Original answer
I was having the same problem and in my case the files weren't being deleted because they were write protected. When I went to delete them manually using rm -r ./storage/framework/cache
I got the warning rm: descend into write-protected directory 'cache/c5'?
. I wasn't going to type yes for every file in the cache so I ran the same command as sudo & it worked without a hitch sudo rm -r ./storage/framework/cache
.
This answers your question as to why they aren't being deleted by Artisan cache:clear
& running rm
is an easy enough work-around; although it doesn't solve the problem of why the files are being written as write-protected.
After deleting the cache Laravel again creates the cache as write-protected. This means it is probably a bug & requires someone to submit a bug report to the Laravel developers. Since the work-around is trivial I'll leave that for someone else to do.
Solution 2
You can try:
php artisan config:cache
It solve most of my problems.
Solution 3
You can also use Tinker:
php artisan tinker
Cache::store("file")->flush()
Solution 4
I ran into similar issue recently. The cache files created through Cache
facade seems to persist even after running php artisan cache:clear
and as mentioned by @Precastic, is turned out to be a file permission issue.
Instead of removing the files/folders manually, I just ran the same command with admin privilege like this
sudo php artisan cache:clear
and it worked for me. Hope this helps someone.
Related videos on Youtube
fronzee
10+ years experience as a developer in PHP, JavaScript, SQL, and a litany of other web development technologies. Currently excited to be working with Laravel and Vue. Most of my experience has been in medical research at the UNM Center for HPV Prevention. Now I am CTO and lead developer for More Brothers Construction, where I manage and develop applications used in kitchen remodels, bathroom remodels, and more. Much different than medical research!
Updated on July 09, 2022Comments
-
fronzee almost 2 years
Laravel 5.4 app.
CACHE_DRIVER
is set tofile
andQUEUE_DRIVER
is set tosync
in.env
.When I run
php artisan cache:clear
It saysCache cleared successfully
yet I still have 236K of files in mystorage/framework/cache
directory.Frustrated by this, I also manually deleted all files/directories under
storage/framework/cache
usingrm -rf *
from that directory.Now, when I run
art queue:restart
I get[ErrorException] file_put_contents(/var/www/vhosts/my-app.com/releases/28/storage/framework/cache/ee/2f/ee2f842aa7bb1f53ed
f3a2ed2c09a1807ffa6c90): failed to open stream: No such file or directorySo, I have two problems on my hands. First is: why aren't all the cache files deleted by Artisan? How do I safely delete them? Second problem is: how do I recover from this so that
php artisan queue:restart
doesn't error out on me?UPDATE: It occurred to me that I probably have no reason to restart a queue worker if
QUEUE_DRIVER
is set tosync
, so skipping that command altogether resolves half my issue. Still not sure how to properly delete those 236K of cache files though.-
Rafay almost 5 yearsdid you manage to find out a solution? i am facing same issue and no luck
-
-
fronzee over 6 yearsThanks, but no luck here. Did not resolve the issue.
-
Precastic over 6 yearsThere are several caches in Laravel &
config:cache
only clears & caches the application config files. The cache files created by the Cache::remember() method is whatcache:clear
should be clearing.config:cache
isn't relevant to OPs problem. -
Andrew about 6 yearsJust manually removing these doesn't break anything? Is this all that cache:clear does, delete all these cache files?
-
Andrew about 6 yearsSeems to work, thanks! Although strange, clearing cache with cache:clear works locally but not on production =/
-
Precastic about 6 years@Andrew From what I have read & as far as I am aware all
cache:clear
does is delete cached files. As to your second point, you might be a member of sudo locally? -
panepeter about 6 yearsLooking into Illuminate\Cache\FileStore.php, what's happening in case of a
flush
is that every directory belowframework/cache/data
gets deleted. Therefore, I'm usingrm -rf framework/cache/data/*
, which also prevents the .gitignore inside cache folder from being removed. -
Summer Developer about 3 yearsA note - doing the short answer can sometimes cause problems with deleted
.gitignore
files. If possible, I much prefer and find it safer to just dosudo php artisan cache:clear
if it's a permissions issue.