jstedfast/MailKit · error · InvalidOperationException

Cannot delete this folder.

Error message

Cannot delete this folder.

What it means

Like rename, IMAP forbids deleting INBOX and namespace roots. QueueDeleteCommand throws InvalidOperationException when IsNamespace is true or the folder has the Inbox attribute, before sending DELETE to the server.

Solutions

  1. Exclude INBOX and namespace roots from deletion logic: check (folder.Attributes & FolderAttributes.Inbox) != 0 && !folder.IsNamespace
  2. Disable delete UI affordances for special folders
  3. Delete only ordinary child folders identified via GetSubfolders filtering

Example fix

// before
if (folder.Subfolders.Count == 0) folder.Delete(); // throws for INBOX
// after
if (folder.Subfolders.Count == 0 && !folder.IsNamespace && (folder.Attributes & FolderAttributes.Inbox) == 0)
    folder.Delete();
Defensive patterns

Strategy: validation

Validate before calling

bool CanDelete(IMailFolder f) => !f.IsNamespace && (f.Attributes & FolderAttributes.Inbox) == 0;
if (CanDelete(folder)) folder.Delete();

Try / catch

try {
    folder.Delete();
} catch (InvalidOperationException) {
    // INBOX/namespace root cannot be deleted; report to user
}

Prevention

When it happens

Trigger: Calling folder.Delete() on the INBOX (Attributes contains FolderAttributes.Inbox) or on a namespace root (IsNamespace == true).

Common situations: Cleanup scripts walking all folders and deleting empties; UIs with a delete button enabled on INBOX; automated tests deleting fixture folders including INBOX.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of jstedfast/MailKit@9d3859a785 (2026-09-15). Data as JSON: /api/errors/02674e7de6710d50. Report an issue: GitHub.

Appendix: source

Thrown at MailKit/Net/Imap/ImapFolder.cs:1320

		/// <exception cref="ImapProtocolException">
		/// The server's response contained unexpected tokens.
		/// </exception>
		/// <exception cref="ImapCommandException">
		/// The server replied with a NO or BAD response.
		/// </exception>
		public override async Task RenameAsync (IMailFolder parent, string name, CancellationToken cancellationToken = default)
		{
			var ic = QueueRenameCommand (parent, name, cancellationToken, out var encodedName);

			await Engine.RunAsync (ic).ConfigureAwait (false);

			ProcessRenameResponse (ic, parent, name, encodedName);
		}

		ImapCommand QueueDeleteCommand (CancellationToken cancellationToken)
		{
			if (IsNamespace || (Attributes & FolderAttributes.Inbox) != 0)
				throw new InvalidOperationException ("Cannot delete this folder.");

			CheckState (false, false);

			return Engine.QueueCommand (cancellationToken, null, "DELETE %F\r\n", this);
		}

		void ProcessDeleteResponse (ImapCommand ic)
		{
			ProcessResponseCodes (ic, this);

			ic.ThrowIfNotOk ("DELETE");

			Reset ();

			if (Engine.Selected == this) {
				Engine.State = ImapEngineState.Authenticated;
				Engine.Selected = null;
				OnClosed ();

View on GitHub (pinned to 9d3859a785)