Laravel Storage Delete Multiple Files Example

By Hardik Savani November 5, 2023 Category : Laravel

Hey Friends,

In this short guide, we will show you laravel storage delete multiple files. let’s discuss about how to remove multiple files in laravel storage. I’m going to show you about laravel store multiple images remove. This example will help you laravel storage remove multiple files. Alright, let us dive into the details.

Laravel Storage facade provides delete() method to remove multiple files from storage folder. so, let's see the simple example code.

let's see the example:

Example 1: Laravel Storage Delete Multiple Files

here is a controller code of delete multiple files using Storage facade.

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.');

}

}

Output:

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.');

}

}

}

Output:

File deleted successfully.

I hope it can help you...

Tags :
Shares