Home / Snippets / How to Delete Directory In Laravel Storage
How to Delete Directory In Laravel Storage cover

How to Delete Directory In Laravel Storage

3.1K

3 years ago

0 comments

To delete a directory in the Laravel application you can make use of the "deleteDirectory()" method available from the Storage facade.
<?php

Storage::deleteDirectory($directory);
This method will delete the directory with all of its files so you must carefully ensure that you are deleting the right directory.
<?php

Storage::deleteDirectory('path/to/directory');

How to Specify Disk when Deleting Directory in Laravel


Since some Laravel applications will have multiple disks you can specify the disk to use like below.
<?php

Storage::disk('s3')->deleteDirectory('path/to/directory');
Do note that you will have to execute the code to delete the directory. One way is to call an endpoint to do so.
<?php

Route::get('/delete-directory', function () {
    Storage::disk('s3')->deleteDirectory('path/to/directory');
});
Now when you visit the "/delete-directory" endpoint, the "path/to/directory" will get deleted.
notion avatar

Alaz

Week-end developer currently experimenting with web, mobile, and all things programming.

Topics:

Frontend

Resource

Average

Average

Support Us

If you like our tutorial, support us by being our Patreon or buy us some coffee ☕️

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this