Delete file from amazon s3 bucket using Laravel

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.

Answers 1
  • Delete a file from amazon s3 bucket using Laravel

    if(Storage::disk('s3')->exists($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

Related queries helps you to understand and may also used for implementation of current code snippet