Posts Learn Components Snippets Categories Tags Tools About
/

How to Delete Directory In Laravel Storage

Learn how to programmatically delete a directory and the file inside it using the Storage facade provided by Laravel.

Created on Sep 01, 2021

3060 views

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.

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

Load comments for How to Delete Directory In Laravel Storage

)