Sample code for 30+ languages & platforms
React Native 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 React Native Downloads

React Native
import { Imap, MessageSet } from '@chilkat/react-native'

async function chilkatExample() {
  // 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.
  const imap = new Imap();

  // Turn on session logging:
  imap.keepSessionLog = true;

  // Connect to an IMAP server.
  // Use TLS
  imap.ssl = true;
  imap.port = 993;
  try {
    await imap.connectAsync('imap.example.com');
  } catch {
    console.log(imap.lastErrorText);
    return;
  }

  // Login
  try {
    await imap.loginAsync('myLogin', 'myPassword');
  } catch {
    console.log(imap.lastErrorText);
    return;
  }

  // Select an IMAP mailbox
  try {
    await imap.selectMailboxAsync('Inbox');
  } catch {
    console.log(imap.lastErrorText);
    return;
  }

  const fetchUids = true;
  // Get the message IDs for all emails having "FTP2" in the subject.
  const messageSet = new MessageSet();
  try {
    await imap.queryMbxAsync('SUBJECT FTP2', fetchUids, messageSet);
  } catch {
    console.log(imap.lastErrorText);
    return;
  }

  // Set the Deleted flag for each message:
  try {
    await imap.setFlagsAsync(messageSet, 'Deleted', 1);
  } catch {
    console.log(imap.lastErrorText);
    return;
  }

  // Expunge and close the mailbox.
  try {
    await imap.expungeAndCloseAsync();
  } catch {
    console.log(imap.lastErrorText);
    return;
  }

  // Display the session log.
  console.log(imap.sessionLog);

  // Disconnect from the IMAP server.
  await imap.disconnectAsync();
}