38

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

8 Answers 8

58

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

for Laravel >= 5.8

    use Illuminate\Support\Facades\Storage;

    // Get all files in a directory
    $files =   Storage::allFiles($dir);

    // Delete Files
    Storage::delete($files);
1
  • 1
    It has been tried with Laravel 5.8 and it is working fine. I used it for my Task Schecduler. Commented Dec 8, 2019 at 22:40
25

Just use it.

 File::cleanDirectory($direction);
2
  • This is the way. Commented Feb 5, 2020 at 2:40
  • 4
    Note he is using the Facade 'File' (use File; or better use Illuminate\Support\Facades\File;). You can also use this in conjunction with Storage disks: File::cleanDirectory(Storage::disk('some-disk')->path(''));
    – Ken
    Commented Aug 10, 2021 at 7:16
7

You can use Filesystem method cleanDirectory

$success = Storage::cleanDirectory($directory);

Please see documentation for more information:

https://laravel.com/api/master/Illuminate/Filesystem/Filesystem.html#method_cleanDirectory

3
  • 6
    Thanks for the hint cleanDirectory but does not actually solve calling those Storage::cleanDirectory giving me an error League\Flysystem\Filesystem::cleanDirectory. Commented Oct 30, 2017 at 3:26
  • 4
    This method does not exist statically on the Storage facade.
    – Soulriser
    Commented Jan 18, 2019 at 14:47
  • 2
    Call to undefined method League\Flysystem\Filesystem::cleanDirectory laravel 6
    – xpredo
    Commented Oct 12, 2019 at 14:16
5

In Laravel 5.8 you can use:

Storage::deleteDirectory('backgrounds');

Remember to include:

use Illuminate\Support\Facades\Storage;
1
  • 3
    Actually, this deletes the directory itself too.
    – Mooncake
    Commented Mar 12, 2019 at 15:01
3

In Laravel 5.7 you can empty a directory using the Storage facade like so:

Storage::delete(Storage::files('backgrounds'));

$dirs = Storage::directories('backgrounds');

foreach ($dirs as $dir) {
    Storage::deleteDirectory($dir);
}

The delete() method can receive an array of files to delete, while deleteDirectory() deletes one directory (and its contents) at a time.

I don't think it's a good idea to delete and then re-create the directory as that can lead to unwanted race conditions.

1

I'm handling this by deleting the whole directory as I don't need it. But if, for any case, you need the directory you should be good by just recreating it:

$d = '/myDirectory'
Storage::deleteDirectory($d);
Storage::makeDirectory($d);
0
0
//You can use Illuminate\Filesystem\Filesystem and it's method cleanDirectory('path_to_directory).
For Example:
    $FolderToDelete = base_path('path_to_your_directory');
    $fs = new \Illuminate\Filesystem\Filesystem;
    $fs->cleanDirectory($FolderToDelete);   
    //For Delete All Files From  Given Directory.
    $succes = rmdir($FolderToDelete);
    //For Delete Directory
    //This Method Works for me

#Laravel 
#FileManager
#CleanDirectory

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.