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

ThanhPv's avatar

Storage:: delete() not working

Hi, I am using this methods to delete files

$request = request(['id', 'path']);
        $id = $request['id'];
        $path = $request['path'];
        File::find($id)->delete();      
        Storage::delete(public_path($path));

I can check the $path correctness by using command line to test the past. But Storage::delete() does do work. Could someone please help? And what should I do when a file's name is with spaces? How to return Storage errors so I know what's is happening?

0 likes
17 replies
nagavinod424's avatar

U have to delete the file first but u r deleting the database record first

1 like
nagavinod424's avatar

I think the path specified is wrong

public_path gives the public folder path

$path should be absolute path from the public folder

then the file will be deleted surely.....

ex : $path = 'uploads\file_name'

Snapey's avatar

You are going to let anyone delete anything?

1 like
Sinnbeck's avatar

@hadihasanpur I can just call this endpoint again and again with different paths, until i have everything deleted. I could start with path = 'index.php' to bring down your site :)

2 likes
ThanhPv's avatar

@nagavinod424

I tried both options but it did not works. test with command line from public folder ok.

@Snapey: I will need do more for authorization. Should not pass the pass via request but I have no idea why the delete() is not working at all. I wish It can give some errors.

thanks

aurawindsurfing's avatar

@ThanhPv I think you over complicating things here. Your delete method should look like this:

Storage::disk('s3')->delete('$path' . $file_path);

The order in which you delete does not matter here since you declare you variables before you delete the record from the DB and you do not actually take them from database at all but from a request.

Hardcode it. See it it works and work backwards and you will find where the problem is. Storage should return true on success.

2 likes
ThanhPv's avatar
ThanhPv
OP
Best Answer
Level 3

Wow php unlink($filename) works so perfectly.

Many thanks

1 like
kwarnkham's avatar

I have the same issue. I tried it in "php artisan tinker" with the exact same path and it works. But not via browser. I think this is permission issue. Is there anyway to do it in laravel's way? Because I don't want to use unlink($filename).

jlrdw's avatar

@kwarnkham

Is there anyway to do it in laravel's way?

Laravel uses symfony components which in turn users php unlink.

Insology's avatar

@kwarnkham use $deleted=Storage::disk('public')->delete($media->filename); with disk('public') i solved the issue

11 likes
irvansc's avatar

I used this method and it worked File :: delete ('image /'. $ Avatar-> avatar); don't forget to Use File;

1 like
Vegas Web Design's avatar

// path to my image file

$filename = public_path() . '/images/' . $property->featured_image;

// then delete the file from the server with:

unlink( $filename );

Please or to participate in this conversation.