Reimport apkg and update existing media

Is your feature request related to a problem? Please describe.

I have an Anki deck I’m programatically generating. My card templates are doing some interactive stuff with JavaScript. To avoid embedding a bunch of
JavaScript in the card template, I’ve chosen to store all the helper functions in a separate _Foo.js media file (the underscore prefix is necessary to get Anki to actually import the media, as suggested here: Field Replacements - Anki Manual ). This works until I make a change to this js file, at which point re-importing doesn’t actually update the js file. IIUC, it is instead creating a new filename with a hash suffix. See add_data_to_folder_uniquely. This cautious approach doesn’t help me as templates still use the original, unsuffixed js file.

This was mentioned before over in Import Media in Importer without updating Notes - #6 by Andr , but that thread is about a different feature request. I thought it would make sense to start a dedicated thread for this. My description of the issue is specific to JavaScript, but IIUC, this would also apply to soind and images as documented here: Field Replacements - Anki Manual .

Describe the solution you’d like

I’d like the ability to tell Anki to clobber existing media files rather than cautiously giving them new names.

Describe alternatives you’ve considered

As I’m generating the deck programatically, I could hash the file contents myself and generate a unique name. This would work, but I suppose would leave orphaned media in my media collection.

Closing thoughts

I don’t actually understand how Anki’s current behavior is reasonable. It seems llike it’s pretty much guaranteed to do something other than what the user wants. I understand that we are dealing with a shared namespace here (blindly clobbering files in the media directory would be dangerous) but silently renaming them (thereby producing media files that nothing references) for me violates the principle of least surprise. I’d rather the import flow call this conflict out and ask me to resolve it myself.

Am I misunderstanding things? Does Anki’s import flow actually make some effort to update references to media when it has to rename them?

Yes. When Anki detects a same-named, but not identical media file on import, it both renames the file by appending a hash and updates the reference to that file in the imported notes. So, the already existing notes will continue to use the already existing file. The newly imported notes (and updated notes?) will use the newly imported file.

[This might not help much with your use case, but I thought I could at least clarify that one point.]

Orphaned media is a normal occurrence, and can easily be cleaned up by running Check Media > Delete Unused Media periodically.

@Danika_Dakika, that doesn’t seem to be happening. Is it possible you’re talking specifically about note fields that reference media? (If you point me to the relevant code, I can debug it.)

In my example, I’m talking about _ prefixed media files referenced from a template. References to them don’t seem to be updated, and the orphaned media detection seems to ignore them.

You’re right, I oversimplified your case too much when responding. What I said applies to regular media files attached/referenced in note fields. _-files that are only referenced in templates exist outside of that framework.

But to bring an _-file inside that framework – it might work to use a dummy-note to reference the file. I doubt that would update the file-reference in a card template, but it might be worth trying.