Copied to clipboard

Flag this post as spam?

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


  • Josip 195 posts 662 karma points c-trib
    Jul 16, 2019 @ 13:30
    Josip
    0

    How to programmatically delete file?

    Hi all,

    I cant find any example how to programmatically delete file (image). I think that i should use DeleteMediaFile(string filepath) but i dont know how to use it to delete for example news image.

    Any help is apriciated.

    Thanks

    Josip

  • Sibren 40 posts 212 karma points c-trib
    Jul 16, 2019 @ 13:36
    Sibren
    100

    You can use Umbraco.Web.Composing.Current.Services.MediaService.Delete(media-item) on a Media-item, given that you'd have the media-item. Otherwise, to get it by ID first: MediaService.GetById(id)

  • David Armitage 510 posts 2082 karma points
    Jun 21, 2020 @ 04:06
    David Armitage
    0

    Hi Guys,

    For completeness here is how I do it for Umbraco 8.

    public void DeleteMedia(int mediaId)
            {
                try
                {
                    IMedia media = _mediaService.GetById(mediaId);
                    if(media != null)
                        _mediaService.Delete(media);
    
                }
                catch (Exception e)
                {
                    _logger.Error<UmbMediaService>("DeleteMedia | Exception: {0} | Message: {1}", e.InnerException != null ? e.InnerException.ToString() : "", e.Message != null ? e.Message.ToString() : "");
                }
            }
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies