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

Delete IMAP Email

To delete an email, the "Deleted" flag must be set to 1 for each message to be deleted. You must also call Expunge or ExpungeAndClose to remove the emails marked as deleted.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

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

-- This example deletes email matching a criteria from Inbox.
local imap = chilkat.Imap.new()

-- Turn on session logging:
imap.keep_session_log = true

-- Connect to an IMAP server.
-- Use TLS
imap.ssl = true
imap.port = 993
local success = imap:connect("imap.example.com")
if not success then
    print(imap.last_error_text)
    return
end

-- Login
success = imap:login("myLogin", "myPassword")
if not success then
    print(imap.last_error_text)
    return
end

-- Select an IMAP mailbox
success = imap:select_mailbox("Inbox")
if not success then
    print(imap.last_error_text)
    return
end

local fetchUids = true
-- Get the message IDs for all emails having "FTP2" in the subject.
local messageSet = chilkat.MessageSet.new()
success = imap:query_mbx("SUBJECT FTP2", fetchUids, messageSet)
if not success then
    print(imap.last_error_text)
    return
end

-- Set the Deleted flag for each message:
success = imap:set_flags(messageSet, "Deleted", 1)
if not success then
    print(imap.last_error_text)
    return
end

-- Expunge and close the mailbox.
success = imap:expunge_and_close()
if not success then
    print(imap.last_error_text)
    return
end

-- Display the session log.
print(imap.session_log)

-- Disconnect from the IMAP server.
success = imap:disconnect()