Sample code for 30+ languages & platforms
AutoIt

Remove a Remote Directory on an SFTP Server

See more SFTP Examples

Demonstrates the Chilkat SFtp.RemoveDir method, which deletes a remote directory. The only argument is the directory path.

Background: Most SFTP servers refuse to remove a directory that is not empty, so deleting a populated tree means removing its files and subdirectories first — the inverse of building a path level by level. There is no single "recursive delete" in the SFTP protocol; a client implements it by listing, descending, and removing bottom-up.

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

;  Demonstrates the SFtp.RemoveDir method, which deletes a remote directory.  The only argument
;  is the directory path.

$oSftp = ObjCreate("Chilkat.SFtp")

;  Connect, authenticate, and initialize the SFTP subsystem.
Local $iPort = 22
$bSuccess = $oSftp.Connect("sftp.example.com",$iPort)
If ($bSuccess = False) Then
    ConsoleWrite($oSftp.LastErrorText & @CRLF)
    Exit
EndIf

;  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.
Local $sPassword = "mySshPassword"

$bSuccess = $oSftp.AuthenticatePw("mySshLogin",$sPassword)
If ($bSuccess = False) Then
    ConsoleWrite($oSftp.LastErrorText & @CRLF)
    Exit
EndIf

$bSuccess = $oSftp.InitializeSftp()
If ($bSuccess = False) Then
    ConsoleWrite($oSftp.LastErrorText & @CRLF)
    Exit
EndIf

;  Delete a remote directory.  Most SFTP servers require the directory to be empty first.
$bSuccess = $oSftp.RemoveDir("subdir/oldfolder")
If ($bSuccess = False) Then
    ConsoleWrite($oSftp.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite("Directory removed." & @CRLF)

$oSftp.Disconnect