Sample code for 30+ languages & platforms
React Native

Delete an IMAP Mailbox

See more IMAP Examples

Demonstrates the Chilkat Imap.DeleteMailbox method, which deletes a mailbox from the IMAP server. This deletes the mailbox itself, not merely the messages it contains. This example deletes a mailbox named OldFolder.

Background: Deleting a mailbox removes the folder and everything in it — an irreversible action, so applications typically confirm first. Server rules can add constraints: some refuse to delete a folder that still has child folders, or that is currently selected. This is the counterpart to CreateMailbox; to remove individual messages rather than a whole folder, mark them deleted and Expunge.

Chilkat React Native Downloads

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

async function chilkatExample() {
  // Demonstrates the Imap.DeleteMailbox method, which deletes a mailbox from the IMAP server.
  // This deletes the mailbox itself, not merely the messages it contains.

  const imap = new Imap();

  imap.ssl = true;
  imap.port = 993;

  try {
    await imap.connectAsync('imap.example.com');
  } catch {
    console.log(imap.lastErrorText);
    return;
  }

  try {
    await imap.loginAsync('user@example.com', 'myPassword');
  } catch {
    console.log(imap.lastErrorText);
    return;
  }

  // Delete a mailbox (folder) from the server.
  try {
    await imap.deleteMailboxAsync('OldFolder');
  } catch {
    console.log(imap.lastErrorText);
    return;
  }

  console.log('Mailbox deleted.');

  try {
    await imap.disconnectAsync();
  } catch {
    console.log(imap.lastErrorText);
    return;
  }
}