Example of Laravel Storage Delete Multiple Files

Posted by

We will demonstrate how to delete numerous files from Laravel storage in this little post. Let’s talk about how to delete several files from a Laravel storage location. I’ll demonstrate how to remove numerous photos from a Laravel store. You may remove numerous files using laravel storage by using this example. Okay, let’s get into the specifics.

To remove numerous files from a storage folder, use the delete() method provided by the Laravel Storage facade. let’s examine the straightforward example code.

let’s see the example:

Example 1: Laravel Storage Delete Multiple Files

app/Http/Controllers/FileController.php

<?php
    
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
use File;
    
class FileController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        Storage::delete(['upload/test.txt', 'upload/test2.txt', 'upload/test3.txt']);
    
        dd('Files deleted successfully.');
    }
}

Example 2: Laravel Storage Delete File If Exists

here is a controller code of delete file using Storage facade.

app/Http/Controllers/FileController.php

<?php
    
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
    
class FileController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        if(Storage::exists('upload/test.txt')){
            Storage::delete('upload/test.txt');
            dd('File deleted successfully.');
        }else{
            dd('File does not exist.');
        }
    }
}
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x