Copied to clipboard

Flag this post as spam?

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


  • Daniel 44 posts 187 karma points
    Aug 21, 2014 @ 11:15
    Daniel
    0

    Clearing media cache for deleted files

    I have a page where I list all files that exists in a specific media folder. The files are not added manually to the document node, but listed programmatically with a surface controller, like this:

    var children = Umbraco.TypedMedia(model.Node).Children;
    model.Files = children.Where(c => c.DocumentTypeAlias.Equals("File")).OrderBy(c => c.Name);

    When a file is added it's all fine and dandy, it shows up in the list as expected. But when a file is deleted it still shows up in the list. 

    I know I can delete the umbraco.config file to clear the cache, but it's not really an acceptable solution since editors will add and remove files all the time. Is there a way to clear the (media) cache programmatically or manually via the umbraco interface?

    Thanks,
    Daniel

  • Dan Lister 416 posts 1974 karma points c-trib
    Aug 21, 2014 @ 11:27
    Dan Lister
    2

    Hi Daniel,

    The media cache should be up to date even when items have been deleted. I guess if you wanted to make sure, you could hook into the delete media event and refresh the cache from there. I'm fairly certain the core events do this anyway.

    using System;
    using Umbraco.Core;
    using Umbraco.Core.Services;
    using Umbraco.Web.Cache;
    
    namespace Application
    {
        public class MediaCacheRefresherEventHandler : ApplicationEventHandler
        {
            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                MediaService.Deleted 
                    += (sender, args) => DistributedCache.Instance.RefreshAll(new Guid(DistributedCache.MediaCacheRefresherId));
            }
        }
    }
    

    Thanks, Dan.

  • Daniel 44 posts 187 karma points
    Aug 21, 2014 @ 13:40
    Daniel
    0

    Hi Dan,

    Thanks for your input. To do this change I would need to rebuild the core lib, which seems a bit overkill and seems like it could hurt the posibility to upgrade later on. Right?

    I tried adding this line into the controller that fetches the files, but it didn't make any difference.

    DistributedCache.Instance.RefreshAll(newGuid(DistributedCache.MediaCacheRefresherId));

    Thanks,
    Daniel 

  • Robert J. Bullock 386 posts 405 karma points
    Aug 08, 2017 @ 21:37
    Robert J. Bullock
    1

    I have this problem myself with 7.6.3. We delete a media file but cannot get it to stop being listed no matter what we do. The actual file on disk remains behind as well.

Please Sign in or register to post replies

Write your reply to:

Draft