Check Media considers static media used in templates unused

I have an image file that I use in one of my card types’ templates. I put it into Anki’s media folder, and then used it in the template without any issue. However, since it is not present on any cards’ fields, going to Tools > Check Media shows it as unused, and “Delete Unused” would break my existing card templates. I only have a single static media file like this so it’s not a big deal to copy it back to Anki’s media folder after clearing out unused media, but I imagine it would be a big pain for people who have more.

An easy way to get around this would be to check all the src attributes in all of the card templates, and count any files used there “used.” This would fix the issue not only for image files, but for audio files as well.

I haven’t contributed to Anki before, but I can try and make a pull request to fix this if that’s okay!

This is (somehow) intended, and in the manual it is written than putting media directly inside the templates is not supported (for this reason). If you don’t want a media to be considered unused, you can simply prefix its name with an underscore.

That’s indeed not too complex, but not that easy either (at least if you really want to be exhaustive). After all, you can’t “simply” parse the HTML as is, as there are Anki’s syntax in there, and also the DOM might be modified in an arbitrary way once the card is rendered.

2 Likes

Thanks for the response! I didn’t realize that you can prevent media from being flagged as unused by prefixing the file name with an underscore, that seems to be a much more foolproof solution. Though, if that’s the case, wouldn’t putting media directly in templates be supported, since you just have to prefix the file names with an underscore? Or is there some other issue with putting media in templates I’m not aware of

I’m not aware of any either. Maybe it’s not “officially” supported because it somehow bypasses Anki’s normal media checking, meaning that if, for instance, you remove from the template the media you just prefixed with an underscore, then it’s effectively not going to be used any longer, but Anki still won’t show it to you as unused. Thus this requires you understand, up to a certain point, what is happening, and to handle your media “by hand”.

2 Likes

I just exported a deck and it seems that Anki is actually capable of checking whether or not a media file is used in a template to some extent. In the apkg file, it included one of the underscored files that’s only used in templates. Furthermore, I created another underscored file in the media folder that isn’t used in any templates and tried reexporting, and it wasn’t included in the apkg. So, it does seem that Anki is capable of detecting the use of media files in templates somewhat, but in a different context.