title: How to delete older image from public folder on upload using Laravel
body: <p>I have folder in my public folder profile_images where my images are being stored. I want to delete older image from this folder when new image is uploaded.</p>
  <p>How I can delete my older file on upload?</p>
  <p>Here is my whole controller code:</p>
  <pre><code>public function uploadImage(Request $request)
  {
      $user_id = $request-&gt;input('user_id');
      $image = $request-&gt;input('image');
      $r = [
          'user_id' =&gt; $user_id,
  
      ];
      $validator = Validator::make($r, [
          'user_id' =&gt; 'required|exists:users,id',
  
      ]);
      if($validator-&gt;fails()) {
          return response(['status' =&gt; false, 'message' =&gt; 'Validation Errors', 'errors' =&gt; $validator-&gt;errors()-&gt;all()], 500);
      }
  
      if ($validator-&gt;fails()) {
          return response([
              'status' =&gt; false,
              'message' =&gt; __('messages.validation_errors'),
              'errors' =&gt; $validator-&gt;errors()-&gt;all()], 200);
      }
      try {
          $path = public_path('profile_images');
          @mkdir($path, '0777', true);
  
          $image = base64_decode($image);
  
          $imageName = str_random(10).'.'.'png';
          Storage::disk('profile-image')-&gt;put($imageName, $image);
  
          $path = asset('public/profile_images/' . $imageName);
  
          $this-&gt;userBasicInfo-&gt;where('user_id', $user_id)-&gt;update(['profile_pic' =&gt; $path]);
  
          return response(['status' =&gt; true, 'message' =&gt; 'Image Uploaded successfully', 'data' =&gt; ['profile_image' =&gt; $path]], 200);
  
      } catch (\Exception $ex) {
          return response(['status' =&gt; false, 'message' =&gt; $ex-&gt;getMessage()], 500);
  
      }
  }
  </code></pre>