153

Laravel was displaying to me "Access denied for user 'homestead'@'localhost' (using password: YES)". One solution for this was clearing the cache and the config cache stored, all this with these three commands:

php artisan cache:clear
php artisan config:clear
php artisan config:cache

After php artisan cache:clear, terminal says:

Failed to clear cache. Make sure you have the appropriate permissions. (with red background)

Doing the second and third code (php artisan config:clear and php artisan config:cache) works fine! But it still gives me the error when typing the first line. Can anyone explain why?

E.Akio
  • 2,249
  • 3
  • 14
  • 27
  • 5
    It worked for me when I changed the sequence (route:clear, config:clear, then cache:clear). – Ihab Shoully May 10 '20 at 02:13
  • 1
    Use sudo if none of the suggested solutions work for you. ;) – Shafi Sep 24 '20 at 03:39
  • 2
    `sudo -u www-data php artisan cache:clear` – doox911 May 05 '21 at 13:36
  • 3
    This is honestly a problem every single time... im so tired of this problem... i have like a full page of notes on how to fix this, but it seems like the list is getting longer and longer... this time around i cant fix it at all.. deleted all cached files, created all the folders, tried to halt, tried to reload provision, tried to open terminal in administrator, tried to switch my cache drivers.... tried it all, this time it just wount do the storage link, so annoying – ii iml0sto1 Aug 25 '21 at 09:11

29 Answers29

423

If the data directory doesn't exist under (storage/framework/cache/data), then you will have this error.

This data directory doesn't exist by default on a fresh/new installation.

Creating the data directory manually at (storage/framework/cache) should fix this issue.

Kazmi
  • 1,198
  • 9
  • 20
Mytho XY
  • 4,262
  • 1
  • 6
  • 2
  • Right on the spot! I'm complete with your answer – E.Akio Sep 14 '18 at 14:21
  • 64
    I have `storage/framework/cache/data` and am still getting this message. – Jay Bienvenu Aug 07 '19 at 14:48
  • @JayBienvenu It should work without the data directory anyway - only `storage/framework/cache` this path must already exist (at least for newer Laravel versions 5.7+) - check if you are not overriding the default storage path and if not, check if your cache folder has sufficient permissions for webserver/php ; assuming default FileStore cache - the issue is probably happening in `vendor/laravel/framework/src/Illuminate/Cache/FileStore.php` - method `flush()` - you can start your debugging from there :) – jave.web Sep 23 '19 at 17:15
  • 1
    @JayBienvenu, try this https://stackoverflow.com/a/54442312/2140408 – Sid Aug 03 '21 at 09:51
  • Still having this error too... soooooo annoying... tried basically everything in my long list of possible solutions to this error.. – ii iml0sto1 Aug 25 '21 at 09:15
  • When I upgrade laravel 5.8 to 6 then I faced this problem. Solution is correct. – kodmanyagha Mar 11 '22 at 07:07
134

Try deleting these cached files under bootstrap folder:

/bootstrap/cache/packages.php
/bootstrap/cache/services.php
/bootstrap/cache/config.php

Then run php artisan cache:clear

Mahdi Bashirpour
  • 17,147
  • 12
  • 117
  • 144
Sid
  • 4,302
  • 3
  • 26
  • 27
  • 1
    Cool...Its working, good option when you are working on cpanel and you dont have rights for terminal. – dipenparmar12 Mar 13 '20 at 15:22
  • I tried everything, my error was `In Find.php line 287: Authentication failed. ` config:cache, cache:clear config:clear, my cache was empty, nothing works. You save me, thanks! – Rodrigo Jun 10 '21 at 14:49
  • Dident work.. still cant do art storage:link – ii iml0sto1 Aug 25 '21 at 09:31
  • 1
    Thank you very much. This worked for me. I already had `storage/framework/cache/data` so above didn't work. – MoDzeus Nov 14 '21 at 16:00
60

Calling

php artisan config:cache 

before

php artisan cache:clear

fixed the issue.

Martin Cup
  • 2,399
  • 1
  • 21
  • 32
44

Just only add folder named data in storage/framework/cache/ and try:

php artisan cache:clear
MBT
  • 21,733
  • 19
  • 84
  • 102
Kamlesh
  • 5,233
  • 39
  • 50
42

Calling the following 4 commands should fix most of the permission issues on laravel.

sudo chown -R $USER:www-data storage
sudo chown -R $USER:www-data bootstrap/cache
chmod -R 775 storage
chmod -R 775 bootstrap/cache

Basically, chown -R $USER:www-data what this does is it set current user $USER as owner and www-data as group and chmod -R 775 gives 7 to user,7 to group and 5 to other.

#PS: You need to run above command from the laravel project directory, else, you need to provide full path like /var/www/project_name/storage

Saroj Shrestha
  • 2,696
  • 4
  • 21
  • 45
  • the problem with this approach is that it keeps resetting to not-working. – Sumit Wadhwa Nov 29 '21 at 07:53
  • @SumitWadhwa I don't think that's a problem, with the approach, let's say if you go with different user, or updated the permission while pulling code, then it might not work, else it should be fine. Instead, if you don't want to give permissions to current user, you could also another approach by adding user to group `www-data`, like: `sudo usermod -a -G examplegroup exampleusername`, and you just have to give permissions to `www-data` thereafter. – Saroj Shrestha May 21 '22 at 06:42
  • 1
    Thank you. In attempting to fix something else, I had set both user and group to www-data, which apparently was causing the specified issue. This fixed it. Thanks! – dgo Apr 02 '23 at 20:58
38

enter image description here

First try:

Check if there is a "data" folder inside "storage/framework/cache/". If there is not, then create it manually. (create a new folder with name "data")

Option 2:

If there is a "data" folder inside "storage/framework/cache/". Remove ALL existing folders inside there.

Then final, running this:

php artisan cache:clear

This should fix this issue: "Failed to clear cache. Make sure you have the appropriate permissions."

Raymond
  • 1,309
  • 14
  • 10
21

In Laravel 8 I solved my issue by first running composer dump-autoload and then used php artisan config:cache

Abid_niazi_15
  • 825
  • 8
  • 6
  • 4
    Works like a charm! – Ajith Gopi Jul 30 '21 at 17:35
  • This appears to be the solution for Laravel 8 with Homestead and Vagrant. I'm sure it works in other environments as well, but this did the trick for me with these two. The other comments are largely valid when it comes to setting permissions however when doing `ls- l` to see ownership if everything appears correct this step of regenerating autoload files appears to be the quickest way to flush out anything wonky that is too hard to get eyes into. – Jem Sep 19 '21 at 21:44
  • Worked perfectly for me without requiring any permission changes, thanks! I just ran "php artisan config:cache" first and then I was able to successfully run "php artisan cache:clear". – bstonehill Sep 29 '21 at 21:42
  • Laravel 5.6 also works! – tanteng Dec 16 '22 at 02:47
9

You should update the permission using the below steps:

  1. Check user using command "whoami" then let output is "ironman"
  2. Run below command if "data" folder exists in the cache directory

sudo chown -R ironman:ironman storage/framework/cache/data/

This will resolve your below issueenter image description here

Ranjan Fadia
  • 1,363
  • 1
  • 14
  • 18
  • 1
    this is the only proper solution. Others are maybe correct in some cases. Maybe this solution can be apply on "storage & bootstrap/cache" – Ali Özen Apr 10 '23 at 21:53
  • @AliÖzen Thanks for your genuine comment, you can Upvote this answer to support me. – Ranjan Fadia Apr 12 '23 at 07:39
  • 1
    This works when you're unable to delete the `storage/framework/cache/data/` (I'm using Sail and L9)! Make sure to replace "ironman" with the output of the first command of course – pu4cu Jul 22 '23 at 15:18
  • @pu4cu good to know that it is working for Laravel 9 – Ranjan Fadia Jul 24 '23 at 07:49
8

Deleting and adding back the ./storage/framework/cache/data folder worked for me.

Thomas Jones
  • 96
  • 1
  • 2
5

Delete all subfolders under:

storage/framework/cache/data/
Skywalker
  • 1,717
  • 1
  • 22
  • 25
3

In my case the problem was I had 'CACHE_DRIVER=memcached' in .env but didn't have memcached installed. Switching to the file driver or installing memcached fixed the problem for me.

Step29
  • 51
  • 1
  • 4
  • In addition you need to start memcached as if not it will simply fail with the "Failed to clear cache. Make sure you have the appropriate permissions." – Alex Aug 05 '22 at 06:11
  • It was the solution for me. Thanks a lot @Step29! – Petitemo Apr 25 '23 at 13:45
2

I ran my project in a docker container, then later tried accessing it via laragon, I had similar issue, this was due to compiled configurations in /bootstrap/cache/config.php.

I fixed fit by running php artisan config:clear, this deletes the /bootstrap/cache/config.php file automatically.

2

I had the same problem but noticed if you run php artisan config:clear it also by default runs cache:clear right after it so when you run it again there is not cache in it and gives that error. you only need to run php artisan config:clear. I am not sure why cache:clear fails when its ran alone but running the config:clear is a good alternative.

Here is a helpful alias i use to clear everything in the app.

laraclear='php artisan config:cache && php artisan config:clear && php artisan view:clear && php artisan route:clear && php artisan telescope:clear && php artisan debugbar:clear'

Remove any unwanted commands that you do not use in it.

Dharman
  • 30,962
  • 25
  • 85
  • 135
2

Can't this solve it?

$php artisan optimize:clear
Compiled views cleared!
Application cache cleared!
Route cache cleared!
Configuration cache cleared!
Compiled services and packages files removed!
n00
  • 41
  • 2
2

(For MAC Users)

Sometimes, it means current user don't have sufficient permission to the storage/framework/cache/data/ folder. Run

  1. sudo chmod -R ug+rwx storage/framework/cache/data/

then

  1. php artisan cache:clear

Hope sometimes it work for you.

radrow
  • 6,419
  • 4
  • 26
  • 53
Junayed
  • 91
  • 6
1

You may need to clear the autoloader with composer dump-autoload

If that doesn't work you can manually remove the following non-tracked (usually) files to clear the autoloader and cache if they get jammed up:

/bootstrap/cache/packages.php

/bootstrap/cache/services.php

jeremykenedy
  • 4,150
  • 1
  • 17
  • 25
  • I've seen this answer (of deleting both packages and services files from bootstrap/cache folder) on another topic, but why should it be a resolution? Plus, there was no changes after removing them or using dump-autoload. – E.Akio Sep 08 '18 at 16:11
  • If the autoloader gets jammed up with cached configuration settings that are not what you want such as the DB connections, you can get into a place where they cannot clear. – jeremykenedy Sep 08 '18 at 16:22
1

Giving 775 permission to the storage directory solved this problem for me.

sudo chmod -R 775 storage
habib
  • 1,454
  • 17
  • 31
1

For Laravel Homestead on Windows:

In your .env file, change your CACHE_DRIVER from file to memcached.

Run php artisan cache:clear.

1

Solution : update the permission :

  1. get user name by this command : whoami

  2. Run command if "data" folderexists in the cache directory.

    sudo chown -R YourUSERName:YourUSERName storage/framework/cache/data/

0

Had the same problem on my vagrant/homestead VM. All the other things in this thread didn't help.

The solution was vagrant reload --provision

0

I am running my project in Homestead.

My environment :

Ubuntu1~20.04+ Laravel 6.20.26

My output

enter image description here

display real fatal info

Edit src/Illuminate/Filesystem/Filesystem.php 598:14 original code:

if (! $preserve) {
    @rmdir($directory);
}

debug code

if (! $preserve) {
    rmdir($directory);
}

Then re-execute php artisan cache:clear The output changed:

enter image description here

Then, I just resolve Text file busy

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
zzr
  • 11
  • 3
0

Ensure APP_URL is properly set on .env file. Changing http to https in the APP_URL variable on .env file solved it for me.

SEYED BABAK ASHRAFI
  • 4,093
  • 4
  • 22
  • 32
Bravo Gidi
  • 11
  • 3
0
  1. In my case, there is no cache available that's why the artisan is unable to delete or reset the cache.

  2. First browse the website and allow it to create some cache then try to clear it.

  3. You can check whether the cache is available or not in directory storage/framework/cache/*

0

For my case, I have all the directory, but we have 2 users.

  1. deployer
  2. www-data

The cache path are created with www-data, but the deployer has no write access . So we need to give the access to the deployer user.

setfacl -R -m u:deployer:rwx /var/www/html
Rithy
  • 184
  • 2
  • 3
  • 14
0

You just need to UPDATE the cache folder owner:

First check the current user with: whoami // output: miratech

Second run sudo chown -R mirad:mirad storage/framework/cache/

Finally re-run php artisan cache:clear

This error: enter image description here

Will be FIXED and you should see something similar to:

enter image description here

MiraTech
  • 1,108
  • 18
  • 35
0
Ubuntu 20.04 
My Problem is.
UnexpectedValueException 

There is no existing directory at "/home/mehedihasansagor/work-place/all-code/nik/storage/logs" and it could not be created: Permission denied

When I removed these files, Then the problem is solved

config.php
packages.php
routes-v7.php
services.php

php artisan optimize:clear
-1

got the same error. try giving chmod 777 permission in the concerned folder

-2

My guess is you have a permission/ownership problem. Either set up the permissions correctly or recursively delete the cache folder manually and re-create it:

sudo rm -Rf storage/framework/cache
mkdir storage/framework/cache
Rishi Ranjan
  • 165
  • 2
  • 8
-3

Incase non of these works for you, simply run your php artisan cache:clear command with sudo. e.g. sudo php artisan cache:clear Thank me later~!

Devmaleeq
  • 541
  • 6
  • 9