Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.



Get Started Now!

Example of Laravel Storage Delete Multiple Files

Laravel

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