Recursive delete with the command line windows FTP client

If you’re dead set on finding a way to do a recursive delete with Windows’ standard command line FTP client you’re in the wrong place. Actually, the whole internet is the wrong place for you. Why? It’s simply impossible to find a way to do recursive deleting of directories in ftp.exe on the Internet. Well, I’ve searched high and low and couldn’t find it that is. If you can, I’d love to hear from you.

What I did find however is a way to achieve the same thing with a simple different FTP client.

At my job someone was looking for a way to delete all contents from a certain directory. This directory contains files and more directories (which in turn can contain more files and even more directories). This is easy if you’re working in a graphical FTP client like FileZilla but in our case it had to be done with a script. This meant we needed to work from the command line. Windows’ standard FTP client simply didn’t work. As long as a directory is not empty, it can’t be deleted. There are probably complicated scripts that can be written to do what we want but we’re no guru’s and we couldn’t find them by means of Google.

After a lot of searching we finally found our answer. I knew that the linux based FTP client LFTP does what we want with a built in command. All I had to was find a way to run it in Windows. Finally Google was my friend again. It didn’t take too long for me to find a windows version of LFTP. It’s in a ZIP file and it can be downloaded from here.

When running the included install.bat, Windows complained a little but that didn´t stop the program from working. I did need to specify the full path to the exe (for instance C:\Windows\lftp.exe) in order to run it. Now you could write a script like this:

debug 10
open ftp.blah.org
user <user> <pass>
rm -r <directory>

And after saving the script you can start LFTP like so:

C:\Windows\lftp.exe -f <script_file>

I assume you’ll understand that you’ll need to replace all text that is enclosed <like this> with their corresponding values (so replace <user> with the username to connect with and <pass> with the user’s password)?

For more info on using LFTP try the documentation or this page (with scripting examples).

Please let me know in the comments if you’ve found other ways to achieve the same thing (perhaps with Windows’ standard FTP client?). Hope this helps!

10 comments on Recursive delete with the command line windows FTP client

  1. Hi, I was very interested in using this to deploy websites, but the windows version of lftp seems to have disappeared off the internet. Any idea where it is, or could you somehow make it available? Thanks for any help.

  2. Hi Daniel,

    I still had the zip just in case this would happen. I updated the link in my post. Will this get me in trouble though? It’s open source right?

    Good luck with it!

    Walter

  3. Hi Walter, that was very good of you. Thank you very much!

  4. Hi Daniel,

    This method almost works for me, but then errors out. When I run the script, it moves to the directory I specify, deletes the files in that directory, and then attempts to change to the first subdirectory. But this fails and I get the following error: “The filename, directory name, or volume label syntax is incorrect” The only difference between my script and yours is I’m using mrm instead of rm.

    Would you have any ideas on why it is having trouble changing directories? The sub directory name is “SubDirOne” so I don’t think it’s the directory name that is causing a problem. Thanks.

  5. Hi Terry (my name’s Walter btw),

    I wouldn’t know. I’m certainly not an expert in this field. I’d try to find some lftp support forum if I were you.

    Walter

  6. research mdelete. it is your friend.

  7. lftp seems to still be available in cygwin

  8. Be sure to run:
    set ftp:list-options “-a”

    Or else files beginning with “.” will not be processed, and nor will you be able to delete folders containing those files.

  9. Did you ever figure out how to get rm/mrm to work recursively?

  10. Hi Dave, I’m afraid I didn’t personally. I was just hoping that this would serve as a starting point for others. Good luck!

Leave a response: