Sample code for 30+ languages & platforms
DataFlex

Expunge Deleted Messages from a Mailbox

See more IMAP Examples

Demonstrates the Chilkat Imap.Expunge method, which permanently removes all messages marked with the \Deleted flag from the currently selected mailbox. The mailbox remains selected. This example marks a message deleted with SetFlag and then expunges.

Background: IMAP deletion is two-phase, much like POP3. First a message is marked with the \Deleted flag (it still exists, just tagged); then EXPUNGE permanently removes every flagged message and renumbers the rest. Splitting the two steps lets a client mark several messages and remove them together, or unmark (undelete) before committing. Expunge keeps the mailbox selected; ExpungeAndClose does the same and then closes it.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoImap
    String sTemp1

    Move False To iSuccess

    //  Demonstrates the Imap.Expunge method, which permanently removes all messages marked with
    //  the \Deleted flag from the currently selected mailbox.  The mailbox remains selected.

    Get Create (RefClass(cComChilkatImap)) To hoImap
    If (Not(IsComObjectCreated(hoImap))) Begin
        Send CreateComObject of hoImap
    End

    Set ComSsl Of hoImap To True
    Set ComPort Of hoImap To 993

    Get ComConnect Of hoImap "imap.example.com" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComLogin Of hoImap "user@example.com" "myPassword" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Select the mailbox to operate on.
    Get ComSelectMailbox Of hoImap "Inbox" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Mark message (sequence number 1) with the \Deleted flag.  The 4th argument (1) sets the
    //  flag; the 2nd argument (False) means the id is a sequence number, not a UID.
    Get ComSetFlag Of hoImap 1 False "\Deleted" 1 To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Permanently remove all \Deleted messages from the selected mailbox.
    Get ComExpunge Of hoImap To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Expunged the deleted messages."

    Get ComDisconnect Of hoImap To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End



End_Procedure