Delete file from amazon s3 bucket using Laravel

Updated at 07-Feb-2022, By samar

Delete file from amazon s3 bucket using Laravel

Through the use of the programming language, we will work together to solve the "Delete file from amazon s3 bucket using Laravel" puzzle in this lesson.

You can delete files from amazon s3 bucket using Laravel. You have to call the storage Facades with disk('s3’) and pass the object key or file path to delete method.
  • Delete a file from amazon s3 bucket using Laravel

    //Syntax: 
    Storage::disk('s3')->delete('path/file.jpg'); 
    if(Storage::disk('s3')->exists($path)) {
        Storage::disk('s3')->delete($path);
    }
    //For example your path to file or object key will look like this.
    $path = 'uploaded_files/d486cebd-746c-4533-a53f-a0f8e77e15e2.jpg';
    

    It will delete the file from s3 bucket. File path is the key of an object (file) in amazon s3 bucket. You can get the key of the object after clicking on the object inside the folder. You can also store the key of the object in the table and delete the object (document) after passing the key to delete method dynamically. 

    To avoid exceptions you can check file exists or not in amazon s3 bucket using exists method.

    Additional Info:

    Use Storage facade

    use Illuminate\Support\Facades\Storage;

Back to code snippet queries related laravel