3

I'm new to Orchard CMS and C# development. I'm trying to create my own News Module. The module injects a few MenuItems into the AdminMenu and provides functionality to manage site news. I've created a NewsItem document type, which consists of TitlePart, BodyPart and my own NewsPart.

NewsPart is based on NewsPartRecord which holds 2 extra string fields SourceTitle and SourceUrl.

I am faced with the problem that I can't remove entirely my content items. ContentManager.Remove(id) just marks the item version as latest = false and hides it. But I don't need to store old versions and any backups for my document items. It is like a garbage in the DB.

Using the OnRemove event in my NewsPartHandler affects only the newsPartRecord table, and other parts like TitlePart, BodyPart and item data and versions are still present in database.

How do I fully remove ContentItem data?

forsvarir
  • 10,749
  • 6
  • 46
  • 77
teran
  • 3,214
  • 1
  • 21
  • 30

3 Answers3

3

You don't: Orchard only does soft deletes. This way you can undelete, and revert to previous versions. It's technically possible to clean the database of deleted items, but to my knowledge nobody bothered to do it.

Bertrand Le Roy
  • 17,731
  • 2
  • 27
  • 33
  • thanks for your answer! I read about soft deletes, but it seems strange to me as it leads to some garbage in DB. after a week of learning Orchard the whole concept seems overcomplicated to me, and looks like it designed for end users, not for developers. – teran Oct 21 '14 at 18:55
  • Does it cause an actual problem? – Bertrand Le Roy Oct 22 '14 at 07:50
  • 1
    it depends on type of site, in some cases may be. – teran Oct 22 '14 at 08:02
  • 2
    I disagree with that. Orchard is IMO designed for developers, not for end users. – devqon Oct 22 '14 at 12:56
  • 2
    Though I too think that a hard delete should exist, especially to delete really old data or data of content types that don't even exist anymore. – devqon Oct 22 '14 at 12:56
  • @teran: can you be more specific? what problem would that be? – Bertrand Le Roy Oct 23 '14 at 06:57
  • @BertrandLeRoy let suppose there is a system where we have thousands of incoming documents per day and for some reasons we should delete half of them. then after few years we have millions of garbage records in DB. In case we should process data in DB (build reports or something like that) it will decrease speed. but actually in such projects we should choose other tools, not orchard. – teran Oct 23 '14 at 08:08
  • @devqon I said "not for developers" because there are a lot of coding in custom modules to show your list of documents, you should implement controllers to List, Create, Edit, remove documents. this looks very strange to me. I think Orchard should do all of these for me, and I should only describe document structure and templates. In case I need custom document list view, I should override this, but not to write every time. – teran Oct 23 '14 at 08:14
  • That's a supposition, from your own words. Are you in such a situation? – Bertrand Le Roy Oct 26 '14 at 04:56
  • Although offtopic, I couldn't agree with @devqon more. If anything, Orchard is primarily designed for developers.. – Nathan Jan 17 '16 at 09:03
1

Based on ViRuSTriNiTy's answer, this handler will destroy any content item and all its part's records when anyone of them is removed:

public class PermanentRemoveContentHandler : ContentHandler
{
    private readonly IOrchardServices _orchardServices;

    public PermanentDeleteContentHandler(IOrchardServices orchardServices)
    {
        this._orchardServices = orchardServices;
    }

    protected override void Removed(RemoveContentContext context)
    {
        this._orchardServices.ContentManager.Destroy(context.ContentItem);
    }
}

Note that for custom code, you can call IContentManager.Destroy() instead of the usual IContentManager.Remove() when you want to delete everything explicitly.

Community
  • 1
  • 1
Maxime Rossini
  • 3,612
  • 4
  • 31
  • 47
0

Use IContentManager.Delete() instead and implement IContentHandler which overrides Destroyed() to remove dependencies like part data. This should do the trick.

ViRuSTriNiTy
  • 5,017
  • 2
  • 32
  • 58