Copied to clipboard

Flag this post as spam?

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


  • Søren Kottal 713 posts 4571 karma points MVP 6x c-trib
    Feb 27, 2020 @ 13:56
    Søren Kottal
    0

    ContentService.Delete() not deleting the node

    I am trying to delete specific nodes from the recycle bin using contentService.Delete(IContent node).

    The returning OperationResult has Success = true, but the node still exists in the recycle bin.

    Here is my code:

                var updatedBefore = DateTime.Now.AddDays(-30);
                var nodes = _contentService.GetPagedContentInRecycleBin(0, int.MaxValue, out long totalRecords, new Query<IContent>(scope.SqlContext).Where(x => x.UpdateDate < updatedBefore));
    
                foreach (var node in nodes)
                {
                    _contentService.Delete(node);
                }
    

    I know I can use .EmptyRecycleBin()to empty the recycle bin, but I only want nodes older than X days to be deleted.

    Is this a bug, or am I doing something wrong?

  • Anders Bjerner 487 posts 2995 karma points MVP 8x admin c-trib
    Feb 27, 2020 @ 14:44
    Anders Bjerner
    0

    We've used to do the same in V7, although mostly for the media archive:

    var mediaService = Services.MediaService;
    var files = mediaService.GetMediaInRecycleBin();
    foreach (IMedia file in files)
    {
        if ((DateTime.Now - file.UpdateDate).Days > 60)
        {
            mediaService.Delete(file);
        }
    }
    

    So it would say it's a bug if similar doesn't work in V8.

    How are you confirming whether the items has in fact been deleted? Running the code again or checking manually in the backoffice whether the items have been deleted? I think there has been some issues in the past with lookups in the content service being cached, so that could explain why.

  • Søren Kottal 713 posts 4571 karma points MVP 6x c-trib
    Feb 27, 2020 @ 17:18
    Søren Kottal
    0

    Both running the code again and checking manually in the backoffice.

  • Søren Kottal 713 posts 4571 karma points MVP 6x c-trib
    Feb 27, 2020 @ 17:26
    Søren Kottal
    100

    Forgot to complete the scope 🤦‍♂️ #h5is :)

  • iNETZO 134 posts 497 karma points c-trib
    Jun 09, 2021 @ 15:31
    iNETZO
    0

    I've got the same problem, using autoComplete: true but because it was not working i've also added scope.Complete(). But they are still not deleted. Do i miss something in my code?

    using (var scope = this.scopeProvider.CreateScope(autoComplete: true))
    {
        var mediaRecords = this.mediaService.GetPagedMediaInRecycleBin(0, int.MaxValue, out long totalMediaRecords, new Query<IMedia>(scope.SqlContext).Where(x => x.UpdateDate < updatedBefore));
    
        foreach (var mediaRecord in mediaRecords)
        {
            try
            {
                this.mediaService.Delete(mediaRecord);
                this.LogToFile(string.Format("Media {0} ({1}) deleted.", mediaRecord.Name, mediaRecord.Id));
            }
            catch (Exception ex)
            {
                this.LogToFile(string.Format("Error deleting Media {0} ({1}): {2}.", mediaRecord.Name, mediaRecord.Id, ex.Message));
            }
        }
    
        scope.Complete();
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft