|
Hi all, I'm trying to permanently delete a content item and all associated records from the database in Orchard Core. I'm currently using: The ContentItem is no longer available through the Orchard core admin application after calling RemoveAsync(), however I'm still seeing records remaining in the database, including entries in tables/indexes related to the content item (for example Document and ContentItemIndex). Is there a recommended Orchard Core way for performing a full permanent delete and cleaning up all related records/indexes? Any guidance or example would be greatly appreciated. Thanks |
Replies: 2 comments
|
I think there's a Prune feature that has been added in v3 https://docs.orchardcore.net/en/latest/reference/modules/Contents/#content-version-pruning |
|
What you see is by design:
Keep the using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Records;
using YesSql;
public class ContentPurger
{
private readonly IContentManager _contentManager;
private readonly ISession _session;
public ContentPurger(IContentManager contentManager, ISession session)
{
_contentManager = contentManager;
_session = session;
}
public async Task PurgeAsync(ContentItem contentItem)
{
// Normal removal: handlers run (routes, search indexes, workflows, ...).
if (!await _contentManager.RemoveAsync(contentItem))
{
return; // a handler cancelled the deletion
}
// Every stored version of the item, whatever its Published/Latest state.
var versions = await _session
.Query<ContentItem, ContentItemIndex>(x => x.ContentItemId == contentItem.ContentItemId)
.ListAsync();
foreach (var version in versions)
{
// Deletes the Document row and its map index rows.
_session.Delete(version);
}
await _session.SaveChangesAsync();
}
}Inside a request or a background task the session is also committed when the shell scope ends, so the explicit This is permanent, so make sure nothing else still references the item. If the Audit Trail module is enabled, its events for the item (which hold a snapshot of the content) are separate documents and are not touched by this; they are removed by the Audit Trail trimming setting. About the pruning feature mentioned above (Content Version Pruning, in 3.0): it deletes archived versions, so it also picks up items removed with For deleting specific items immediately, the code above is the more direct option. |
What you see is by design:
RemoveAsyncis a soft delete. It loads the versions that are published or latest, setsPublished = falseandLatest = falseon them, saves them and runs the removal handlers. Nothing is deleted fromDocumentor from the index tables; every version simply stays there as an "archived" version.OrchardCore/src/OrchardCore/OrchardCore.ContentManagement/DefaultContentManager.cs
Lines 903 to 946 in f6040cb