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: 12.4 build 677 for .NET 4.5
Syntax
public bool DeleteFolder(
	string folderName
)

Parameters

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.
Exceptions
ExceptionCondition
MailBeeExceptionAn error occurred and ThrowExceptions is true.
Remarks

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.

Examples
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.Connect("imap4.company.com");
imp.Login("jdoe@company.com", "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();
    folders.Reverse();

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

            // Check folder names in case-insensitive manner...
            if (folderName == "inbox" ||
                folderName == "sent" ||
                folderName == "drafts")
            {
                processedCount++;
            }
            else
            {
                try
                {
                    // ...but pass original name when deleting.
                    imp.DeleteFolder(imapFolder.Name);

                    // Remember this deletion attempt succeeded.
                    processedCount++;
                    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")
                    {
                        throw;
                    }
                }
            }
        }
    }

    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.
        break;
    }
}

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

imp.Disconnect();
See Also