Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel File Storage delete all files in directory

Tags:

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'); 
like image 609
ßiansor Å. Ålmerol Avatar asked Oct 30 '17 02:10

ßiansor Å. Ålmerol


People also ask

How do I delete files from storage folder in Laravel 8?

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.

How do you delete a directory in Laravel?

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.

How do you delete a file in Laravel?

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


2 Answers

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'); 
like image 184
ßiansor Å. Ålmerol Avatar answered Oct 25 '22 09:10

ßiansor Å. Ålmerol


    use Illuminate\Support\Facades\Storage;      // Get all files in a directory     $files =   Storage::allFiles($dir);      // Delete Files     Storage::delete($files); 
like image 42
Patrick Chidera Duruamadi Avatar answered Oct 25 '22 10:10

Patrick Chidera Duruamadi