{"record":{"id":"49e07a42eb60aba0","repo":"jstedfast/MailKit","slug":"cannot-rename-this-folder","errorCode":null,"errorMessage":"Cannot rename this folder.","messagePattern":"Cannot rename this folder\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Imap/ImapFolder.cs","lineNumber":1162,"sourceCode":"\t\tImapCommand QueueRenameCommand (IMailFolder parent, string name, CancellationToken cancellationToken, out string encodedName)\n\t\t{\n\t\t\tif (parent == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (parent));\n\n\t\t\tif (object.ReferenceEquals (parent, this))\n\t\t\t\tthrow new ArgumentException (\"Cannot rename a folder using itself as the new parent folder.\", nameof (parent));\n\n\t\t\tif (parent is not ImapFolder || ((ImapFolder) parent).Engine != Engine)\n\t\t\t\tthrow new ArgumentException (\"The parent folder does not belong to this ImapClient.\", nameof (parent));\n\n\t\t\tif (name == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (name));\n\n\t\t\tif (!ImapEngine.IsValidMailboxName (name, DirectorySeparator))\n\t\t\t\tthrow new ArgumentException (\"The name is not a legal folder name.\", nameof (name));\n\n\t\t\tif (IsNamespace || (Attributes & FolderAttributes.Inbox) != 0)\n\t\t\t\tthrow new InvalidOperationException (\"Cannot rename this folder.\");\n\n\t\t\tCheckState (false, false);\n\n\t\t\tstring newFullName;\n\n\t\t\tif (!string.IsNullOrEmpty (parent.FullName))\n\t\t\t\tnewFullName = parent.FullName + parent.DirectorySeparator + name;\n\t\t\telse\n\t\t\t\tnewFullName = name;\n\n\t\t\tencodedName = Engine.EncodeMailboxName (newFullName);\n\n\t\t\treturn Engine.QueueCommand (cancellationToken, null, \"RENAME %F %S\\r\\n\", this, encodedName);\n\t\t}\n\n\t\tvoid ProcessRenameResponse (ImapCommand ic, IMailFolder parent, string name, string encodedName)\n\t\t{\n\t\t\tvar oldFullName = FullName;","sourceCodeStart":1144,"sourceCodeEnd":1180,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Imap/ImapFolder.cs#L1144-L1180","documentation":"Certain folders are structurally non-renamable: namespace root folders and the INBOX (IMAP forbids renaming INBOX). MailKit throws InvalidOperationException after detecting IsNamespace or the Inbox attribute.","triggerScenarios":"Calling Rename on a folder where IsNamespace is true or (Attributes & FolderAttributes.Inbox) != 0 — typically renaming the INBOX itself or a personal-namespace root.","commonSituations":"Bulk rename scripts iterating all subfolders including the namespace root/INBOX; UIs listing INBOX alongside normal folders without marking it special.","solutions":["Skip folders whose Attributes include FolderAttributes.Inbox and namespace roots when iterating","Check folder.IsNamespace and the Inbox attribute before offering rename in UIs","To 'rename' INBOX behavior, create a new folder and MOVE messages instead — IMAP prohibits renaming INBOX"],"exampleFix":"// before\nforeach (var f in personalNamespace) f.Rename(f.ParentFolder, newName(f));\n// after\nforeach (var f in personalNamespace) {\n    if (f.IsNamespace || (f.Attributes & FolderAttributes.Inbox) != 0) continue;\n    f.Rename(f.ParentFolder, newName(f));\n}","handlingStrategy":"validation","validationCode":"bool CanRename(IMailFolder f) => !f.IsNamespace && (f.Attributes & FolderAttributes.Inbox) == 0;\nif (CanRename(folder)) folder.Rename(folder.ParentFolder, newName);","typeGuard":null,"tryCatchPattern":"try {\n    folder.Rename(parent, name);\n} catch (InvalidOperationException) {\n    // folder is INBOX or a namespace root; skip or move contents instead\n}","preventionTips":["Treat INBOX and namespace roots as immutable in folder-management code","Check folder.Attributes before batch rename operations","Model special folders explicitly in your domain layer"],"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"}