Mark Santos
Mark Santos

Reputation: 377

Deleting a folder with files in Laravel

What is the proper syntax when deleting a folder with files in laravel 5.2?

Here is my syntax when deleting the folder from the DB and DIR

$folder = Folder::find($id);
$folder_path = storage_path('locker').'/'. $folder->folder_title;
$folder->delete();
 `rmdir($folder_path);`
\Session::flash('success', 'Folder Deleted!');
return back();

Upvotes: 19

Views: 27737

Answers (3)

Hasibul Hasan
Hasibul Hasan

Reputation: 63

Delete a Folder with Storage or File Class

Storage::deleteDirectory($path);

Laracast

Upvotes: 1

Adiyya Tadikamalla
Adiyya Tadikamalla

Reputation: 967

After much frustration I got the solution.

Use 'File' instead of 'Storage'

File::deleteDirectory($path);

Upvotes: 33

Alexey Mezenin
Alexey Mezenin

Reputation: 163748

The deleteDirectory may be used to remove a directory and all of its files

Storage::deleteDirectory($directory);

https://laravel.com/docs/5.3/filesystem#directories

Upvotes: 20

Related Questions