Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

RafaelMunoznl's avatar

Delete files from storage

I am using the storage directory to uploa the avatars of my users.

The structure is like this: storage/app/public/avatars/file.jpg

I am able to upload avatars without problem, but when the user decides to change the avatar, I need of course, to delete the previous avatar. i have been trying to implement the method described here: https://laravel.com/docs/5.8/filesystem#deleting-files.

I did this:

<?php

namespace App\Models\Client\Traits\Method;

...
use Illuminate\Support\Facades\Storage; // <- Call the facade
...


trait ClientMethod
{

    public static function getPicture($request, $size = false)
    {  

        Storage::delete($request->avatar_location); //<- here I intend to delete the file in https://mydomain.com//storage/avatars/rasputin-1583231825.png
        
       // more code here
    }
}

The file exist in that location. I can download it. Everything looks like the documentation, but it does not get deleted.

Am I missing something?

0 likes
9 replies
RafaelMunoznl's avatar

@sti3bas In my data base I just store this string:

avata_location = 'avatars/misi1-1583236599.jpg'

The full URL is in the $request variable.

When I click on the submit button I get the $requestvariable like this:

    #parameters: array:7 [▼
      "_token" => "6pQ4rchK4KrrO2O85T1bse3ZC1FejA0lggWXNp1v"
      "_method" => "PUT"
      "avatar_type" => "gravatar"
      "client_id" => "1"
      "email" => "[email protected]"
      "avatar_location" => "http://violeta-app.test/storage/avatars/misi1-1583236599.jpg"`// <- url
      "company_id" => "2"
    ]
  }

and that is what I want to delete when I do: Storage::disk('public')->delete($request->avatar_location);

Should I not use the full url to delete whe I use Storage facade?

Sti3bas's avatar
Sti3bas
Best Answer
Level 53

@rafaelmunoznl you should pass the path (avatars/misi1-1583236599.jpg) from the database to delete method and not the full URL.

Sinnbeck's avatar

@rafaelmunoznl imagine if you could delete an image on the internet using its url. Google images would be in huge trouble :)

2 likes

Please or to participate in this conversation.