How to delete folder in laravel?

by darrion.kuhn , in category: PHP General , 2 months ago

How to delete folder in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by wilmer.lemke , 2 months ago

@darrion.kuhn 

To delete a folder in Laravel, you can use the Storage facade. Here is an example of how you can delete a folder:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
use IlluminateSupportFacadesStorage;
use IlluminateFilesystemFilesystem;

...

public function deleteFolder($folderPath)
{
    $filesystem = new Filesystem;
    $filesystem->deleteDirectory(public_path($folderPath));

    // If you want to delete the folder from storage disk:
    // Storage::disk('public')->deleteDirectory($folderPath);
}


Make sure to replace $folderPath with the path to the folder you want to delete. In the above example, the public_path() helper is used to get the full path of the folder within the public directory, but you can customize it based on your folder structure.


Also, note that if you want to delete the folder from a specific storage disk, you can use the deleteDirectory method of the Storage facade instead.


Remember to import the necessary classes at the top of your class file before using them.