{"record":{"id":"20507073572917a9","repo":"OrchardCMS/OrchardCore","slug":"not-a-draft-version","errorCode":null,"errorMessage":"Not a draft version.","messagePattern":"Not a draft version\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore/OrchardCore.ContentManagement/DefaultContentManager.cs","lineNumber":955,"sourceCode":"        foreach (var version in activeVersions)\n        {\n            version.Published = false;\n            version.Latest = false;\n            await _session.SaveAsync(version);\n        }\n\n        await ReversedHandlers.InvokeAsync((handler, context) => handler.RemovedAsync(context), context, _logger);\n\n        return true;\n    }\n\n    public async Task DiscardDraftAsync(ContentItem contentItem)\n    {\n        ArgumentNullException.ThrowIfNull(contentItem);\n\n        if (contentItem.Published || !contentItem.Latest)\n        {\n            throw new InvalidOperationException(\"Not a draft version.\");\n        }\n\n        var publishedItem = await GetAsync(contentItem.ContentItemId, VersionOptions.Published);\n\n        var context = new RemoveContentContext(contentItem, publishedItem == null);\n\n        await Handlers.InvokeAsync((handler, context) => handler.RemovingAsync(context), context, _logger);\n\n        contentItem.Latest = false;\n        await _session.SaveAsync(contentItem);\n\n        await ReversedHandlers.InvokeAsync((handler, context) => handler.RemovedAsync(context), context, _logger);\n\n        if (publishedItem != null)\n        {\n            publishedItem.Latest = true;\n            await _session.SaveAsync(publishedItem);\n        }","sourceCodeStart":937,"sourceCodeEnd":973,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore.ContentManagement/DefaultContentManager.cs#L937-L973","documentation":"DiscardDraftAsync may only be called on a draft: the item must not be Published and must be the Latest version. Anything else throws InvalidOperationException('Not a draft version.'), protecting published or superseded versions from being discarded.","triggerScenarios":"Calling DiscardDraftAsync with a published item (Published=true), an outdated version (Latest=false), or a stale instance superseded by a newer draft/publish.","commonSituations":"UI code caching a published item and later discarding a 'draft'; concurrent edits where another user published; passing an item from AllVersions query results.","solutions":["Load the item with VersionOptions.Latest and verify !Published && Latest before discarding","For published items use UnpublishAsync instead of DiscardDraftAsync","Re-fetch a fresh instance if the passed one may be stale","Guard with an if-check or catch InvalidOperationException to handle race conditions"],"exampleFix":"// before\nvar item = await _contentManager.GetAsync(id, VersionOptions.Published);\nawait _contentManager.DiscardDraftAsync(item);\n// after\nvar item = await _contentManager.GetAsync(id, VersionOptions.Latest);\nif (item is { Published: false, Latest: true })\n{\n    await _contentManager.DiscardDraftAsync(item);\n}","handlingStrategy":"type-guard","validationCode":"var latest = await cm.GetAsync(id, VersionOptions.Latest); bool discardable = latest is { Published: false, Latest: true };","typeGuard":"bool isDraft = contentItem is { Published: false, Latest: true };","tryCatchPattern":"try { await cm.DiscardDraftAsync(item); } catch (InvalidOperationException ex) when (ex.Message == \"Not a draft version.\") { /* item is published or superseded; reload or unpublish instead */ }","preventionTips":["Check Published/Latest flags before discarding","Use UnpublishAsync for published items","Fetch fresh instances instead of reusing stale ones"],"tags":["content-management","versioning","draft"],"backgroundTag":"invalid-state-transition","analyzedSha":"4306c0717fe573f6fca1b4955909ddab6a192807","analyzedAt":"2026-09-13T17:41:05.024Z","contentChangedAt":"2026-09-13T17:41:05.024Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}