Sample code for 30+ languages & platforms
Lua

Remove a Remote FTP Directory (RMD)

See more FTP Examples

Demonstrates the Chilkat Ftp2.RemoveRemoteDir method, which removes a remote directory. The only argument is the remote directory path.

Background: Most FTP servers refuse to remove a directory that is not empty, so clearing a populated folder means deleting its contents first — or using DeleteTree to clear the subtree and then removing the now-empty directory. After a removal, the cached indexed listing is not refreshed automatically; call ClearDirCache before relying on the old cache again.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- Demonstrates the Ftp2.RemoveRemoteDir method, which removes a remote directory.  The only
-- argument is the remote directory path.

local ftp = chilkat.Ftp2.new()

ftp.hostname = "ftp.example.com"
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.
ftp.password = "myPassword"

local success = ftp:connect()
if not success then
    print(ftp.last_error_text)
    return
end

success = ftp:change_remote_dir("public_html")
if not success then
    print(ftp.last_error_text)
    return
end

-- FTP servers generally require the directory to be empty before it can be removed.
success = ftp:remove_remote_dir("uploads/old")
if not success then
    print(ftp.last_error_text)
    return
end

print("Directory removed.")

success = ftp:disconnect()
if not success then
    print(ftp.last_error_text)
    return
end