ImapExpunge Method (String, Boolean)
Permanently removes the messages marked as deleted from the currently selected folder.

Namespace: MailBee.ImapMail
Assembly: MailBee.NET (in MailBee.NET.dll) Version: 11.2.0 build 590 for .NET 4.5
public bool Expunge(
	string uidSet,
	bool forceUidExpunge


Type: SystemString
The comma-separated list of UIDs of the messages to be deleted, or a null reference (Nothing in Visual Basic) if all messages marked as deleted should be removed.
Type: SystemBoolean
If true and uidSet is not a null reference, allows MailBee to use UID EXPUNGE command only to expunge the messages. If false, MailBee is allowed to simulate UID EXPUNGE using standard commands if UID EXPUNGE is not supported by the server.

Return Value

Type: Boolean
true if the command succeeded; otherwise, false.
MailBeeProtocolExtensionNotSupportedExceptionUID EXPUNGE required but not supported by the server and ThrowExceptions is true.
MailBeeExceptionAn error occurred and ThrowExceptions is true.

This overload can be used to expunge the specified messages only. Often, it's required to remove certain message from the folder but leave other messages marked as deleted intact. The standard IMAP4 protocol does not allow this to be done but if the server supports UIDPLUS extension, it becomes possible (via UID EXPUNGE command).

This method always issues EXPUNGE if uidSet is not set. If uidSet is not a null reference, the method will attempt to delete the messages having UIDs from uidSet list and \Deleted flag set. If, however, UIDPLUS is not supported, the method will either simulate effect of UID EXPUNGE using a number of regular IMAP4 commands (if forceUidExpunge is false) or report error and do not expunge any messages (if forceUidExpunge is true).

In most cases, it's ok to set forceUidExpunge to false. The only reason for not doing so is when the component is used with the IMAP folder where messages are added very often by another clients. In this case, while the component executes a sequence of commands to expunge certain messages, another mail client can add, remove, or manage messages in the same folder. In this case, the folder state may go out of sync. In all other cases, if no other mail agents access the same folder at the same time, it's safe to use MailBee simulation of UID EXPUNGE command when it's not supported by the server.

This sample marks the first message in inbox as deleted, and then expunges it. It there are some other messages marked as deleted in inbox, they will not be expunged.
using System;
using MailBee;
using MailBee.ImapMail;

class Sample
    static void Main(string[] args)
        Imap imp = new Imap();

        // Connect to the server and log in the account.
        imp.Login("", "secret");

        // Select Inbox folder.

        // Mark message #1 as deleted.
        imp.SetMessageFlags("1", false,
            SystemMessageFlags.Deleted, MessageFlagAction.Add);

        // To expunge specific message, we need to know its UID.
        EnvelopeCollection uidList = imp.DownloadEnvelopes("1", false, EnvelopeParts.Uid, 0);

        if (uidList.Count > 0)
            // Expunge message #1 by its UID.
            imp.Expunge(uidList[0].Uid.ToString(), false);

        // We do not call Close() here to prevent remaining messages
        // marked as deleted from being expunged.
See Also