Laravel 5.4 app. CACHE_DRIVER
is set to file
and QUEUE_DRIVER
is set to sync
in .env
.
When I run php artisan cache:clear
It says Cache cleared successfully
yet I still have 236K of files in my storage/framework/cache
directory.
Frustrated by this, I also manually deleted all files/directories under storage/framework/cache
using rm -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 directory
So, 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 to sync
, so skipping that command altogether resolves half my issue. Still not sure how to properly delete those 236K of cache files though.
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:
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 gettingput_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 warningrm: 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 hitchsudo rm -r ./storage/framework/cache
.This answers your question as to why they aren't being deleted by Artisan
cache:clear
& runningrm
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.
You can try:
It solve most of my problems.