Posted by Arjun on Saturday 17th February 2018

How to delete file(s) in Laravel 5

How to Laravel 5.x Delete

Deleting files from the file system is very common task nowadays and it can be done in several ways. But here I am gonna show you deleting files in the Laravel way.

We generally use PHP's built-in unlink() function to delete files from the file system, there is nothing wrong using it. But we gonna use Laravel's File::delete() method because it is giving the option to delete multiple files at once.

Note: If there is a problem with deleting a file, the error is silently ignored, so it is always better to check the existence of the file with File::exists().

Delete single file

File::delete($filename);

Delete Multiple file

File::delete($file1, $file2, $file3)

Delete an array of files

 $files = array($file1, $file2);
 File::delete($files);