new_monk has asked for the wisdom of the Perl Monks concerning the following question:

Oh Great Monks.
Is there any way to force a delete on a directory if it contains subdirectories and files in these subdirecotories? Or do I have to go through each sub and delete all the contents first then delete the parent directory.

Here is my code to delete the parent as a reference:
foreach $folder (@iqfolder_arr) { $ftp_disco->rmdir("$folder"); }

That is a code snippet from my script that removes the directory but it only works if the folders are empty. I am trying to do this via ftp b/c I am already using an ftp script to get some files and back them up from a different directory. I want to try and remove folders from the same script.

Replies are listed 'Best First'.
Re: Force a Delete
by ctilmes (Vicar) on Feb 09, 2004 at 18:21 UTC
Re: Force a Delete
by arden (Curate) on Feb 09, 2004 at 18:22 UTC
    I'm afraid to say that I don't know of any ftp server programs that allow a cascading delete. You'll have to go through each folder, do a listing, delete all files, and repeat process for each folder, deleting the parent folder after all children are gone. I think if you do a Super Search you will find some examples of the code you'll need.
        Update: or, as ctilmes mentions, you could allow Net::FTP::Recursive to do the dirty work for you! :^)

    If you have access to the server via a shell, you can issue an 'rm -rf' command if it's unix/linux or 'rmdir /S /Q' if it's windows. You could have the ftp script execute this via a SSH or Telnet connection to keep it all in the same script.

    Update: Since you state in your previous question that you normally telnet into the server, you can use Net::Telnet.

Re: Force a Delete
by Abigail-II (Bishop) on Feb 09, 2004 at 21:40 UTC
    Recursive delete of a directory is not something the RFC mandates servers implement. (Not very surprising if you look at the system call, rmdir(2) doesn't delete non-empty directories). If you insist on doing this via FTP, you have two options:
    • Clean up the directories yourself by having the client traverse the directories, and deleting files.
    • Find, write or modify an FTP server that will support this feature. You might have to modify Net::FTP for that as well.

    Abigail