Sample code for 30+ languages & platforms
PowerBuilder

Recursively Delete a Remote FTP Directory Tree

See more FTP Examples

Demonstrates the Chilkat Ftp2.DeleteTree method, which recursively deletes the files and subdirectories beneath the current remote directory. It takes no arguments. Child directories are removed after their contents; the current root directory itself remains.

Background: FTP has no single recursive-delete command, so this method does the work a client otherwise scripts by hand: descend the tree and remove files and directories bottom-up. It is powerful and irreversible, so the ChangeRemoteDir into the intended directory beforehand is a critical safety step — running it from the wrong place would wipe the wrong subtree. The root directory you are in is left intact; remove it separately with RemoveRemoteDir if desired.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Ftp

li_Success = 0

//  Demonstrates the Ftp2.DeleteTree method, which recursively deletes the files and subdirectories
//  beneath the current remote directory.  It takes no arguments.

loo_Ftp = create oleobject
li_rc = loo_Ftp.ConnectToNewObject("Chilkat.Ftp2")
if li_rc < 0 then
    destroy loo_Ftp
    MessageBox("Error","Connecting to COM object failed")
    return
end if

loo_Ftp.Hostname = "ftp.example.com"
loo_Ftp.Username = "myFtpLogin"

//  Normally you would not hard-code the password in source.  You should instead obtain it
//  from an interactive prompt, environment variable, or a secrets vault.
loo_Ftp.Password = "myPassword"

li_Success = loo_Ftp.Connect()
if li_Success = 0 then
    Write-Debug loo_Ftp.LastErrorText
    destroy loo_Ftp
    return
end if

li_Success = loo_Ftp.ChangeRemoteDir("public_html")
if li_Success = 0 then
    Write-Debug loo_Ftp.LastErrorText
    destroy loo_Ftp
    return
end if

//  Change into the directory whose contents should be removed.
li_Success = loo_Ftp.ChangeRemoteDir("uploads/temp")
if li_Success = 0 then
    Write-Debug loo_Ftp.LastErrorText
    destroy loo_Ftp
    return
end if

//  Recursively delete everything beneath the current directory.  Child directories are removed
//  after their contents; the current root directory itself remains.
li_Success = loo_Ftp.DeleteTree()
if li_Success = 0 then
    Write-Debug loo_Ftp.LastErrorText
    destroy loo_Ftp
    return
end if

Write-Debug "Subtree deleted."

li_Success = loo_Ftp.Disconnect()
if li_Success = 0 then
    Write-Debug loo_Ftp.LastErrorText
    destroy loo_Ftp
    return
end if



destroy loo_Ftp