Sample code for 30+ languages & platforms
Tcl

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 Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

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

set imap [new_CkImap]

CkImap_put_Ssl $imap 1
CkImap_put_Port $imap 993

set success [CkImap_Connect $imap "imap.example.com"]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    exit
}

set success [CkImap_Login $imap "user@example.com" "myPassword"]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    exit
}

#  Select the mailbox to operate on.
set success [CkImap_SelectMailbox $imap "Inbox"]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    exit
}

#  Mark message (sequence number 1) with the \Deleted flag.  The 4th argument (1) sets the
#  flag; the 2nd argument (0) means the id is a sequence number, not a UID.
set success [CkImap_SetFlag $imap 1 0 "\Deleted" 1]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    exit
}

#  Permanently remove all \Deleted messages from the selected mailbox.
set success [CkImap_Expunge $imap]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    exit
}

puts "Expunged the deleted messages."

set success [CkImap_Disconnect $imap]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    exit
}


delete_CkImap $imap