I am trying to delete a folder on my server via sftp. I am using php 7.4.13 and laravel 7.30.4
I have set up my sftp drive and everything (upload, delete files, create directory) works fine.
But the only problem is that I could not delete a folder.
I am using deleteDirectory($directory)
function which is written on the official laravel documentation:
https://laravel.com/docs/7.x/filesystem
Here is my set up:
filesystem.php
'disks' => [
'sftp' => [
'driver' => 'sftp',
'host' => env('SFTP_HOST'),
'username' => env('SFTP_USERNAME'),
'password' => env('SFTP_PASSWORD'),
'timeout' => env('SFTP_TIMEOUT'),
'port' => env('SFTP_PORT'),
]
Here is my code:
Storage::disk('sftp')->deleteDirectory('path/to/folder')
But the folder still exists and all files inside the folder also still exist. There is no error message. And also if I dd()
the code above, it returns true
.
I have all permission to create, write and delete on this server. And also I can delete folder on this server using WinSCP via sftp.
Could anyone tell me which part I did wrong or what function I can use to achieve this?
Thanks a lot and I would provide more information if you need it.
TLDR:
I tried: Storage::disk('sftp')->deleteDirectory('path/to/folder')
but nothing happend.
EDIT 2: Tried to delete all files inside the folder first then delete the folder
$files = Storage::disk('sftp')->allFiles('QCS/test_folder');
Storage::disk('sftp')->delete($files);
try { Storage::disk('sftp')->deleteDirectory('QCS/test_folder', true); } catch (Exception $e) { dd($e->getMessage()); }
EDIT 3: Tried to remove the second parameter of deleteDirectory()
function
$files = Storage::disk('sftp')->allFiles('QCS/test_folder');
Storage::disk('sftp')->delete($files);
try { Storage::disk('sftp')->deleteDirectory('QCS/test_folder'); } catch (Exception $e) { dd($e->getMessage()); }
For both EDIT 2 and EDIT 3:
file inside the folder got deleted but the file still exist, and also no error ;(
2
Answers
Even if the documentation doesn’t show it,
deleteDirectory()
accepts a second parameter$preserver
@src/Illuminate/Filesystem/Filesystem.php
When the second Parameter
$preserve
istrue
, the method will empty the folder without deleting it.In your case,
Storage::disk('sftp')->deleteDirectory('QCS/test_folder', true);
you are asking it to keep the folder.If you want to delete the base folder too, remove the second parameter
The semantics for deleting a directory on a POSIX system are that it fails if the directory is not empty (rmdir). There are a lot of layers of abstraction between your PHP code and the remote filesystem, but I would expect that code implementing the functionality would still conform to the standard behaviour. However I see that the documentation you linked explicitly says otherwise. I suspect the documentation is wrong and the implementation is correct.
You could very this by checking the audit logging on the remote system.
Or it may simply be a permissions issue. Did you check?