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

Stano's avatar
Level 1

Can't delete image from storage

Hello, i can't delete image from storage on hosting but on local server is it working. I use next code. File is exist but delete is impossible.

if (Storage::disk('public')->exists($imagePath1)) { Storage::disk('public')->delete($imagePath1); }

0 likes
7 replies
tisuchi's avatar

@stano Make sure the user has the right permission to delete the file.

And what is the disk that you configured? Is it the public disk?

Stano's avatar
Level 1

Disk is payed hosting. Storage directory has this permissions:

drwxr-xr-x 5 uid3258458 gid3258458 5 Jun 4 11:23 storage

Snapey's avatar

try

Storage::disk('public')->delete(Storage::disk('public')->path($imagePath1)); 

But first, check, what does $imagePath1 contain?

Stano's avatar
Level 1

It not work. Here part of my code:

$gallery = Gallery::find($request->input('post_id'));

        if( $gallery ){
            $imagePath1 = Str::replace('storage', '', $gallery->imgPath1); 
            $imagePath2 = Str::replace('storage', '', $gallery->imgPath2);
            $imagePath3 = Str::replace('storage', '', $gallery->imgPath3);
            $imagePath4 = Str::replace('storage', '', $gallery->imgPath4);
            $imagePath5 = Str::replace('storage', '', $gallery->imgPath5);
            $imagePath6 = Str::replace('storage', '', $gallery->imgPath6);
            $imagePath7 = Str::replace('storage', '', $gallery->imgPath7);
            $imagePath8 = Str::replace('storage', '', $gallery->imgPath8); 
            
            //dd($imagePath1);

            if (Storage::disk('public')->exists($imagePath1)) 
            {
                
                Storage::disk('public')->delete($imagePath1);
            }
            if (Storage::disk('public')->exists($imagePath2)) {Storage::disk('public')->delete($imagePath2);}
            if (Storage::disk('public')->exists($imagePath3)) {Storage::disk('public')->delete($imagePath3);}
            if (Storage::disk('public')->exists($imagePath4)) {Storage::disk('public')->delete($imagePath4);}
            if (Storage::disk('public')->exists($imagePath5)) {Storage::disk('public')->delete($imagePath5);}
            if (Storage::disk('public')->exists($imagePath6)) {Storage::disk('public')->delete($imagePath6);}
            if (Storage::disk('public')->exists($imagePath7)) {Storage::disk('public')->delete($imagePath7);}
            if (Storage::disk('public')->exists($imagePath8)) {Storage::disk('public')->delete($imagePath8);}

            $gallery->delete();
        }

Condition if (Storage::disk('public')->exists($imagePath1)) is true.

Snapey's avatar

why not just show whats in the field like asked?

Stano's avatar
Level 1

here is output:

"/images/m8Yc1ijekJaJCuZyDzIVzQnvea196iq1WDAsIBlw.jpg" // app/Http/Controllers/GalleryController.php:110

of tihis:

if (Storage::disk('public')->exists($imagePath1)) { dd($imagePath1); Storage::disk('public')->delete($imagePath1); }

In filesystems.php is set this:

'public' => [ 'driver' => 'local', 'root' => storage_path('app/public'), 'url' => env('APP_URL').'/storage', 'visibility' => 'public', 'throw' => false, ],

Save image via storage working correctly.

Please or to participate in this conversation.