{"record":{"id":"02674e7de6710d50","repo":"jstedfast/MailKit","slug":"cannot-delete-this-folder","errorCode":null,"errorMessage":"Cannot delete this folder.","messagePattern":"Cannot delete this folder\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Imap/ImapFolder.cs","lineNumber":1320,"sourceCode":"\t\t/// <exception cref=\"ImapProtocolException\">\n\t\t/// The server's response contained unexpected tokens.\n\t\t/// </exception>\n\t\t/// <exception cref=\"ImapCommandException\">\n\t\t/// The server replied with a NO or BAD response.\n\t\t/// </exception>\n\t\tpublic override async Task RenameAsync (IMailFolder parent, string name, CancellationToken cancellationToken = default)\n\t\t{\n\t\t\tvar ic = QueueRenameCommand (parent, name, cancellationToken, out var encodedName);\n\n\t\t\tawait Engine.RunAsync (ic).ConfigureAwait (false);\n\n\t\t\tProcessRenameResponse (ic, parent, name, encodedName);\n\t\t}\n\n\t\tImapCommand QueueDeleteCommand (CancellationToken cancellationToken)\n\t\t{\n\t\t\tif (IsNamespace || (Attributes & FolderAttributes.Inbox) != 0)\n\t\t\t\tthrow new InvalidOperationException (\"Cannot delete this folder.\");\n\n\t\t\tCheckState (false, false);\n\n\t\t\treturn Engine.QueueCommand (cancellationToken, null, \"DELETE %F\\r\\n\", this);\n\t\t}\n\n\t\tvoid ProcessDeleteResponse (ImapCommand ic)\n\t\t{\n\t\t\tProcessResponseCodes (ic, this);\n\n\t\t\tic.ThrowIfNotOk (\"DELETE\");\n\n\t\t\tReset ();\n\n\t\t\tif (Engine.Selected == this) {\n\t\t\t\tEngine.State = ImapEngineState.Authenticated;\n\t\t\t\tEngine.Selected = null;\n\t\t\t\tOnClosed ();","sourceCodeStart":1302,"sourceCodeEnd":1338,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Imap/ImapFolder.cs#L1302-L1338","documentation":"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.","triggerScenarios":"Calling folder.Delete() on the INBOX (Attributes contains FolderAttributes.Inbox) or on a namespace root (IsNamespace == true).","commonSituations":"Cleanup scripts walking all folders and deleting empties; UIs with a delete button enabled on INBOX; automated tests deleting fixture folders including INBOX.","solutions":["Exclude INBOX and namespace roots from deletion logic: check (folder.Attributes & FolderAttributes.Inbox) != 0 && !folder.IsNamespace","Disable delete UI affordances for special folders","Delete only ordinary child folders identified via GetSubfolders filtering"],"exampleFix":"// before\nif (folder.Subfolders.Count == 0) folder.Delete(); // throws for INBOX\n// after\nif (folder.Subfolders.Count == 0 && !folder.IsNamespace && (folder.Attributes & FolderAttributes.Inbox) == 0)\n    folder.Delete();","handlingStrategy":"validation","validationCode":"bool CanDelete(IMailFolder f) => !f.IsNamespace && (f.Attributes & FolderAttributes.Inbox) == 0;\nif (CanDelete(folder)) folder.Delete();","typeGuard":null,"tryCatchPattern":"try {\n    folder.Delete();\n} catch (InvalidOperationException) {\n    // INBOX/namespace root cannot be deleted; report to user\n}","preventionTips":["Filter special folders out of delete candidates","Disable delete actions for INBOX and namespace roots in UIs","Guard cleanup scripts with attribute checks before Delete"],"tags":["imap","mailkit","invalid-operation","inbox"],"backgroundTag":"invalid-state-transition","analyzedSha":"9d3859a7855e3e17582c07fd01972b8e262bf176","analyzedAt":"2026-09-15T15:46:11.592Z","contentChangedAt":"2026-09-15T15:46:11.592Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}