Jamie
Jamie

Reputation: 10886

Laravel remove directory

When I try to delete a directory in Laravel 5.1 like this:

use Storage;

public function deleteUser(Request $request)
{
    $user = User::find($request->id); 
    Storage::deleteDirectory('/files/'.$user->id);
    $user->delete();
    $user->websites()->delete();
    return redirect('/')->with('status','Customer removed');
}

The directory isn't going away. But the user is successfully removed.

Here is the documentation:

https://laravel.com/docs/5.2/filesystem#deleting-files

Why is this not working?

Upvotes: 2

Views: 4230

Answers (2)

Luke Snowden
Luke Snowden

Reputation: 4196

function deleteDirectory( $path ) {
     File::cleanDirectory( $path );
     Storage::deleteDirectory( $path );
     rmdir( $path );
}

Upvotes: 1

Crabigator360
Crabigator360

Reputation: 1032

I was stuck with the Storage:deleteDirectory command too. Later I found out it could be easily done with the following command:

use Illuminate\Support\Facades\File; File::deleteDirectory('/Users/zawad/web/storage/app/sleepdata/DirectoryToBeDeleted');

The argument is the absolute path to the directory which is to be deleted. You can find the root of the path by running the pwd command in the terminal.

Upvotes: 3

Related Questions