Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Nicolas 4 posts 85 karma points
    Mar 06, 2024 @ 11:11
    Nicolas
    0

    Programatically deleting unpublished content

    Hi,

    I've added the functionality to mass unpublish content that came from an api call (roughly 800 entries which are document types with a template under a listing page).

    I've been using the UmbracoHelper & ContentService for getting/creating the entries. However, after running the mass unpublish command, I've been left with 800 unpublished entries that I don't want to delete manually.

    The UmbracoHelper & ContentService don't seem to work with unpublished content (as they use the IPublishedContent interface), so I'm left trying to figure out how to retrieve the unpublished content. Any ideas?

    Thanks!

  • Marcio Goularte 388 posts 1360 karma points
    Mar 07, 2024 @ 12:51
    Marcio Goularte
    100

    Hi Nicolas,

    You can do this via Examine and InternalIndex and loop through these unpublished nodes and remove via ContentService.

    try use IExamineManager https://docs.umbraco.com/umbraco-cms/reference/searching/examine/examine-manager

    Probably inject IExamineManager

    private readonly IExamineManager _examineManager;
    

    try to get index internal

    if (!_examineManager.TryGetIndex(UmbracoIndexes.InternalIndexName, out IIndex index))
    

    And create search query

     var searcher = index.Searcher;
                        var criteria = searcher.CreateQuery(IndexTypes.Content, BooleanOperation.And);
                        var query= criteria.NodeTypeAlias("MyNodeType");
    

    the field you need query will be isPublished

    This link has a code that can help. For your needs, you would change the indexname and make a query for unpublished

    https://www.umbrajobs.com/blog/posts/2021/june/umbraco-9-examine-basic-search/

    another article https://www.jondjones.com/learn-umbraco-cms/umbraco-9-tutorials/getting-started-with-umbraco-9/how-to-build-a-search-page-in-umbraco-v9/

  • Nicolas 4 posts 85 karma points
    Mar 07, 2024 @ 16:17
    Nicolas
    1

    Worked like a charm, thanks!

Please Sign in or register to post replies

Write your reply to:

Draft