Posts Learn Components Snippets Categories Tags 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

17 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.

Snippets by

PostSrc poster

PostSrc

Full stack developer mainly focusing on the front end side of the web. The main author of PostSrc and loves writing tutorial and guides online.

Load comments for How to Delete Directory In Laravel Storage

new

PostSrc Code Components

Collection of Tailwind CSS components for everyone to use. Browse all of the components that's right for your project.

View Components

Other Code Snippets

More