Sample code for 30+ languages & platforms
AutoIt Requires Chilkat v11.0.0+

Delete All IMAP Email

Demonstrates two ways to delete all email in a mailbox on an IMAP server.

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

;  This example requires the Chilkat API to have been previously unlocked.
;  See Global Unlock Sample for sample code.

$oImap = ObjCreate("Chilkat.Imap")

;  Turn on session logging:
$oImap.KeepSessionLog = True

;  Connect to an IMAP server.
;  Use TLS
$oImap.Ssl = True
$oImap.Port = 993
$bSuccess = $oImap.Connect("imap.example.com")
If ($bSuccess = False) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
EndIf

;  Login
$bSuccess = $oImap.Login("myLogin","myPassword")
If ($bSuccess = False) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
EndIf

;  Select an IMAP mailbox
$bSuccess = $oImap.SelectMailbox("Inbox.RubyMail")
If ($bSuccess = False) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
EndIf

;  Get the complete set of Uids for email in the selected mailbox.
$oMessageSet = ObjCreate("Chilkat.MessageSet")
$bSuccess = $oImap.QueryMbx("ALL",True,$oMessageSet)
If ($bSuccess = False) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
EndIf

;  Set the Deleted flag for each message.
;  (ExpungeAndClose must be called to finalize the delete.)
$bSuccess = $oImap.SetFlags($oMessageSet,"Deleted",1)
If ($bSuccess = False) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
EndIf

;  Alternatively, the Deleted flag may be set for each UID
;  individiually, but this is less efficient:
Local $i = 0
Local $iN = $oMessageSet.Count
While $i < $iN
    $bSuccess = $oImap.SetFlag($oMessageSet.GetId($i),$oMessageSet.HasUids,"Deleted",1)
    If ($bSuccess = False) Then
        ConsoleWrite($oImap.LastErrorText & @CRLF)
        Exit
    EndIf

    $i = $i + 1
Wend

;  Expunge and close the mailbox.
$bSuccess = $oImap.ExpungeAndClose()
If ($bSuccess = False) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
EndIf

;  Display the session log.
ConsoleWrite($oImap.SessionLog & @CRLF)

;  Disconnect from the IMAP server.
$bSuccess = $oImap.Disconnect()