Laravel 5.4 - php artisan cache: clear does not clear cache files when using the "file" cache driver

Laravel 5.4 app. CACHE_DRIVER set to file , and QUEUE_DRIVER set to sync in .env .

When I run php artisan cache:clear It says Cache cleared successfully , but I still have 236K 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 this 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
[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: why aren't all the cache files deleted by Artisan? How to safely remove them? The second problem is this: how can I recover from this so that php artisan queue:restart does not cause an error on me?

UPDATE: It occurred to me that I probably have no reason to restart the queue QUEUE_DRIVER if QUEUE_DRIVER set to sync , so skipping this command completely fixes half of my problem. However, you do not know how to delete these 236K cache files correctly.

+5
source share
2 answers

Short answer

Use sudo: sudo rm -r ./storage/framework/cache

Long answer

Make sure that all processes that write to the cache use the same user (and not only belong to the same group), because it turns out that Laravel writes cache files with privileges, something like lines 0755, which restricts the record to the owner.

If you, like me, you use a different user for each of them:

  • PHP process
  • Artisan CLI
  • Artisan through the supervisor (for assignments)

As a result, you get files belonging to different users, and cannot be written or deleted by other users, even if they belong to the desired group (for example, www-data).

Hopefully someone finds a way to set the new file cache privileges in Larvel to about 0775. It would be nice if he just inherited from his parent.

Side note

This for me also caused a problem with Cache::remember() between the supervisor process and the PHP process, so I got put_file_contents errors because the cached files could not be written by different users.

Original answer

I had the same problem and in my case the files were not deleted because they were write protected. When I deleted them manually using rm -r ./storage/framework/cache , did I get a warning rm: descend into write-protected directory 'cache/c5'? . I was not going to enter yes for each file in the cache, so I ran the same command as sudo and worked without a hitch sudo rm -r ./storage/framework/cache .

This answers the question of why they are not deleted by Artisan cache:clear , and running rm is a fairly simple job; although it does not solve the problem of why files are written as write protected.

After deleting the cache, Laravel again creates the cache as write-protected. This means that this is probably a bug, and requires someone to send a bug report to Laravel developers. Since the workaround is trivial, I will leave it for someone else.

+4
source

You can try:

 php artisan config:cache 

It solves most of my problems.

0
source

Source: https://habr.com/ru/post/1270659/


All Articles