Laravel 8 - How to delete file from public storage folder

Laravel 8 - How to delete file from public storage folder

In this PHP Laravel Tutorial, I will let you know how to first check if a file exists and then delete file from public folder.

In general web development, you store the file to local disk and save the reference file name in your database but when you delete the reference from the database then it is require to delete the file from the disk.

And in Laravel PHP, It is very easy to check if file exists on given location and delete it from the given path

In Laravel, you can use File Facade or Storage Facade to delete file.

Remove file using File Facade

Syntax:

File::delete(file_path);

But it is very necessary to check if file exists on given location before deleting it and you can easily check from given line of code:

if(File::exists(public_path('upload/demo.png'))){
    //delete the file
}else{
        // File does not exists
}
Remove file using Storage Facade

Syntax:

Storage::delete(file_path);

Use below line of code to check if file exists or not using Storage Facade

if(Storage::exists('upload/demo.png')){
    // delete file
}else{
      // File does not exists
}

Phone: (+91) 8800417876
Noida, 201301