Is there a way to delete all files in specific directory. I'm trying to clear all my files in my created folder backgrounds in storage\app\backgrounds but in docs seems no method for delete all.
Storage::delete('backgrounds\*.jpg');
Remove file using File FacadeFile::delete(file_path); It is good practice to check if a file exists before deleting it. Then the chances of getting errors become less. So you can make the check of the file exist before deleting it.
The public_path() is a method Laravel uses to fetch the folder passed as a parameter to the path of the directory. The Storage facade grants access to the file system of the application so that the deleteDirectory() method can delete the specified directory.
You could use PHP's unlink() method just as @Khan suggested. But if you want to do it the Laravel way, use the File::delete() method instead. $files = array($file1, $file2); File::delete($files);
I don't think if this is the best way to solve this. But I solved mine calling
use Illuminate\Filesystem\Filesystem;
Then initiate new instance
$file = new Filesystem; $file->cleanDirectory('storage/app/backgrounds');
use Illuminate\Support\Facades\Storage; // Get all files in a directory $files = Storage::allFiles($dir); // Delete Files Storage::delete($files);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With