ImapDeleteFolder Method
Deletes an existing folder (mailbox in IMAP4 terms) from the IMAP4 account.

Namespace: MailBee.ImapMail
Assembly: MailBee.NET (in MailBee.NET.dll) Version: 11.2.0 build 590 for .NET 4.5
public bool DeleteFolder(
	string folderName


Type: SystemString
The full name of the folder to be deleted.

Return Value

Type: Boolean
true if the folder was deleted successfully; otherwise, false.
MailBeeExceptionAn error occurred and ThrowExceptions is true.

The developer should specify the full name of the folder (including all parent folders' names if the folder is a a subfolder of another existing folder). See CreateFolder(String) topic for details regarding folder names.

Note Note
If the folder contains subfolders, the mail server may not allow the client to delete it. It's permitted, however, to delete a folder which does not contain any subfolders but does contain some e-mail messages in it. The currently selected folder and Inbox folder cannot be deleted under any circumstances.

To delete a folder asynchronously, see the sample code in BeginExecuteCustomCommand(String, String, AsyncCallback, Object) topic.

This sample connects to the IMAP4 server, logs in the mail account, and removes all folders except Inbox, Sent, Drafts and their subfolders. If certain folders to be removed contain subfolders, these subfolders are removed first.
// To use the code below, import MailBee namespaces at the top of your code
using MailBee;
using MailBee.ImapMail;

// The actual code (put it into a method of your class)
Imap imp = new Imap();

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

FolderCollection folders = null;

// We may need to iterate through folders collection multiple
// times. For instance, if certain folder to be deleted contains
// subfolders, we should delete this subfolders first and then
// try again. However, using folders.Reverse() optimization
// reduces the number of iterations. Without Reverse, subfolders
// usually appear in the end of the list (thus, all parent folders
// which are in the beginning of the list could not be deleted during
// the first iteration). With Reverse, subfolders will come before
// their parent folders in the list. Thus, if we use Reverse and
// the mail server always returns parent folders before their
// subfolders, we'll delete all the folders in a single pass.
for (;;)
    bool deletedAnything = false;
    int processedCount = 0;

    // Get the list of folders in descending order (so that
    // subfolders come first). Alternatively, we could use
    // 'for' loop (instead of 'foreach') with descending
    // counter variable (i.e. i-- instead of i++).
    folders = imp.DownloadFolders();

    foreach (Folder imapFolder in folders)
        if (!imapFolder.IsValid)
            Console.WriteLine("IMAP4 folder list is corrupted");
            string folderName = imapFolder.Name.ToLower();

            // Check folder names in case-insensitive manner...
            if (folderName == "inbox" ||
                folderName == "sent" ||
                folderName == "drafts")
                    // ...but pass original name when deleting.

                    // Remember this deletion attempt succeeded.
                    deletedAnything = true;
                catch (MailBeeImapNegativeResponseException e)
                    // If the server responded with "NO" response,
                    // it usually means the folder contains,
                    // sub-folders so we need to delete them first
                    // (i.e. that's normal situation). If, however,
                    // the response is BAD, this is the real problem,
                    // and we'll re-throw the exception in this case.
                    if (e.CompletionResult != "NO")

    if (processedCount == folders.Count || !deletedAnything)
        // If we successfully processed all the folders or
        // no folders have been deleted during the last iteration,
        // it means we deleted everything we should or could.

// Display the list of folders remained after performing clean-up.
folders = imp.DownloadFolders();
foreach (Folder imapFolder in folders)

See Also