23

I'm trying to remove a directory with lots of files and folders from my private server space. I'm logging on via SFTP fine; I can access the entire directory no problems; I can even delete individual files with rm. But this would take me forever - so I would really like to just do rmdir on the highest folder that I want to remove. But when I do this, I get

Couldn't remove directory: Failure

Any thoughts as to what I might be doing wrong?

Thanks very much, Sam

user1451632
  • 333
  • 1
  • 2
  • 4

2 Answers2

32

In my experience, rmdir prefers to work on an empty directory. If you're trying to delete the directory foo, I would do:

$rm foo/*
$rmdir foo
Chris
  • 1,092
  • 2
  • 11
  • 15
  • 1
    SSH'ing to do rm -rf was the obvious answer. I tried rm -rf on SFTP but it couldn't accept -rf, as you pointed out. (The wildcard trick in SFTP didn't work either.) Anyway, thanks a bunch for your help! – user1451632 May 14 '13 at 13:58
  • That's funny. rm foo/* worked for me in SFTP. Anyway, I'm glad you got it solved! – Chris May 14 '13 at 14:02
  • 1
    Had the same problem and `rm foo/*` wasn't working because `foo/` contained non-empty directories. Luckily `rsync` was allowed too and I created an empty directory _locally_ and then used `rsync -rv --delete empty_local_dir/ host.example.org:/foo/` to remove everything underneath the remote `foo/` directory. Afterwards it was possible to `rmdir foo` via SFTP. – ckujau Jun 21 '19 at 01:13
6

You have not specified, what SFTP client you are using. So I'm assuming OpenSSH SFTP (sftp).

Command rmdir in OpenSSH SFTP client maps directly to SSH_FXP_RMDIR SFTP protocol request. The SFTP spec for version 3 (the one used by OpenSSH) specifically mentions that the SSH_FXP_RMDIR operation may fail, "if the specified directory is not empty" (though it does not seem to mandate it).

If the directory does not have subdirectories, you can use rm foo/* (meaning OpenSSH SFTP command, not shell command) to remove all the files in the directory first. And then use rmdir.

For more complex cases, you will need a smarter SFTP client.
Or if you have a shell access, use rm -r * in the shell.

Martin Prikryl
  • 21,071
  • 9
  • 77
  • 157