Delete file from a specific directory in Laravel local storage
Solution 1
There is multiple ways to delete image
//In laravel
File::delete($image);
//for specific directory
File::delete('images/' . 'image1.jpg');
and other way (Simple PHP)
//Simple PHP
unlink(public_path('storage/image/delete'));
and if you want to delete more than 1 images than
Storage::delete(['file1.jpg', 'file2.jpg']);
//or
File::delete($image1, $image2, $image3);
for more detail about Delete File in Laravel
Solution 2
Use Storage::delete(). The delete
method accepts a single filename or an array of files to remove from the disk.
Storage::delete($file_to_delete);
May be you want to do something like-
$files = Storage::files($path);
Storage::delete($files);
priMo-ex3m
Updated on June 24, 2022Comments
-
priMo-ex3m almost 2 years
I am storing files at local storage. So, in
/storage/app/public
directory.I am storing my files in
/storage/app/public/userId/images
;I used
php artisan storage:link
, so I can access that files in view, having a shortcut to this folder in/public/storage/userId/images
Inside that path I have 2 images -test.jpg
andtest2.jpg
I can't find a response at Laravel documentation, how to delete file
test.jpg
from/public/storage/userId/images
I tried in this way :
$path = 'public/' . $id . '/diploma'; $files = Storage::files($path); return $files;
It returns me :
[ "public/303030/images/test.jpg" "public/303030/images/test2.jpg" ]
Now, how can I call
Storage::delete('test.jpg')
on that array? -
priMo-ex3m over 6 yearsSo, if
$image = 'test.jpg'
. howFile::delete($image)
will know what image to delete, and from each folder? P.S. Please, review my edit -
priMo-ex3m over 6 yearsSo, if $image = 'test.jpg'. how Storage::delete($image) will know what image to delete, and from each folder? P.S. Please, review my edit
-
Sohel0415 over 6 yearsyou need to specify the filename with path
-
priMo-ex3m over 6 years$path = '/public/' . $userId . '/images/test.jpg' ; Storage::delete('$path); Now it works. I tried in your way, but it works just after adding
/public
as well. Thanks for your answer! -
priMo-ex3m over 6 years$path = '/public/' . $userId . '/images/test.jpg' ; Storage::delete('$path); Now it works. I tried in your way, but it works just after adding
/public
as well. Thanks for your answer! -
Sohel0415 over 6 years@priMo-ex3m glad it helps :), accept the one that helps you, that way it can be useful in future for others
-
priMo-ex3m over 6 yearsSure, just waiting for time out. I will accept first one, just cause it was answered earlier. But in fact, answers are equivalent. Thanks for help!
-
Mohamed Raza over 3 years
Storage::disk('public')->delete('img_001.png');
this works