'How to delete old images from public folder on update using Laravel

I want to delete older images in public folder on update:

My Controller code:

try {
    $path = public_path('profile_images');
    @mkdir($path, '0777', true);

    $image = base64_decode($image);

    $imageName = str_random(10).'.'.'png';
    Storage::disk('profile-image')->put($imageName, $image);


    $path = asset('public/profile_images/' . $imageName);

    $this->userBasicInfo->where('user_id', $user_id)->update(['profile_pic' => $path]);

    return response(['status' => true, 'message' => 'Image Uploaded successfully', 'data' => ['profile_image' => $path]], 200);

} catch (\Exception $ex) {
    return response(['status' => false, 'message' => $ex->getMessage()], 500);
}

How can I achieve the delete functionality?



Solution 1:[1]

Try to use this:

File::delete($file_path)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jkdev