Using Syncthing to have an exact copy of collection.media between Anki Desktop and AnkiDroid

I’m writing this post because I had an idea for a workaround for an existing behavior with Anki (which some might consider problematic) and I want to gain some insight by other users that could have already tried this workaround.

The problem: Some of you might have noticed that when a file in collection.media is modified in desktop, the modified file will not be synchronized to AnkiDroid. This behavior is described in the manual (see quote below) (retrieved from https://docs.ankiweb.net/syncing.html#media on 2025-04-22T21:29:18+0000). This behavior has also been reported at this post.

Anki will synchronize any sounds and images used by your notes. It will notice when media has been added, removed or replaced in your media folder, but will not notice if you have made edits to existing files. To get your edits synced, you need to add, remove or replace a file as well.

The workaround: Setup a Syncthing server in my desktop computer and share the collection.media folder with my Android phone, so that my Android phone has an exact copy of the files at collection.media. This folder will be the only one that I would synchronise using Syncthing.

I also wonder if there would be some undesired consequences of having a complete exact copy of collection.media from desktop in Android phone since Anki Desktop and AnkiDroid use different schemas for their databases.

Off the top of my head, some things to consider –

  1. If you’re using the Play Store version of AnkiDroid – and you are running a recent version of Android – you no longer have outside access to your collection.media directory. Can you reach it with this other sync process?

  2. You’ll need to make sure you’re connected to the internet for your alternate sync process too, instead of just for the regular sync process.

  3. Assuming that you’d turn off media syncing on both devices so you don’t have to sync everything twice (or have the processes fighting over which is the correct version of your media file), you will no longer have a copy of your media on AnkiWeb.

  4. The AnkiWeb sync process syncs your entire profile, so you should figure out what there is outside of your collection.media directory that is essential. For instance, what’s up with collection.media.db2? [Not a rhetorical question – I really don’t know!] Does that need to be kept up-to-date via sync as well or does each app manage that separately? If you aren’t using AnkiWeb for your media sync process, will it be kept up-to-date?

  5. An edited file not syncing is a fairly rare occurrence though, isn’t it? Or is your workflow unique in a way that makes this more common? I question this as someone who frequently edits the audio files on my cards, but since I also regularly add/delete/clean-up my media too, I don’t think I’ve ever been caught without an updated file.

This part puzzled me. As far as I know, if you sync the regular way, collection.media is “a complete exact copy” on your devices, and the databases schemas (and databases) are identical.

@Danika_Dakika

If you’re using the Play Store version of AnkiDroid – and you are running a recent version of Android – you no longer have outside access to your collection.media directory. Can you reach it with this other sync process?

I’m using AnkiDroid version 2.20.1 which I downloaded from F-Droid: https://f-droid.org/en/packages/com.ichi2.anki/ . I assume this version is the same as the one available in Play Store.

I have changed the default directory AnkiDroid to a path in my external SD card. In Settings > Advanced, I set “AnkiDroid directory” to /storage/708B-831D/anki-data/test-1/. Since it is a directory in my SD card, I can edit/add/remove files within that directory.

I just confirmed this by creating two files in my desktop computer, and transfer them from my desktop computer to my Android phone using rsync (I started a SSH server in my Android phone using Termux). When the transfer process finished, and I showed the back of the card which used image my-image-1.jpg in AnkiDroid, the image was the one I last sent using rsync.

$ convert wizard: /tmp/wizard.jpg
$ rsync -Psavv /tmp/wizard.jpg phone-primary:/storage/708B-831D/anki-data/test-1/collection.media/my-image-1.jpg
$ convert netscape: /tmp/netscape.jpg
$ rsync -Psavv /tmp/netscape.jpg phone-primary:/storage/708B-831D/anki-data/test-1/collection.media/my-image-1.jpg

Because I can reach that directory using rsync, I will be able to reach that directory using Syncthing.

Assuming that you’d turn off media syncing on both devices so you don’t have to sync everything twice (or have the processes fighting over which is the correct version of your media file), you will no longer have a copy of your media on AnkiWeb.

Thanks. I didn’t think of this. I forgot to mention that I’m using a self-hosted sync server, but your comment is still valid for collection.media that exists in the directory that have the files of the server (in my desktop computer, it is ~/.syncserver/). If I plan to use Syncthing, I’ll have to turn off “Fetch media on sync” so that syncing by AnkiDroid and Syncthing doesn’t conflict with each other.

Does that need to be kept up-to-date via sync as well or does each app manage that separately? If you aren’t using AnkiWeb for your media sync process, will it be kept up-to-date?

Those are good questions. I thought that no file outside collection.media referred to files in those directories, but I just learned that collection.media.db and collection.media.db2 also exist. Perhaps, those databases contain the files that are being used in flashcards, so as to avoid iterating through all fields in all cards to find unused media files (which Anki Desktop might be able to do by simply by reading collection.media.db). I inspected the source code and I found this code which calls _backend.check_media, but I couldn’t find a method called check_media in _backend (permalink), so I couldn’t confirm whether collection.media.db is read when pressing the “Delete Unused” button in “Check Media”.

A related fact: In my Android phone, the file is called collection.media.db.

$ ssh phone-primary ls -la /storage/708B-831D/anki-data/test-1/
total 768
-rwxrwx--- 1 root everybody      0 Apr 23 15:00 .nomedia
drwxrwx--- 2 root everybody 131072 Apr 23 15:00 backup
-rwxrwx--- 1 root everybody 139264 Apr 23 15:00 collection.anki2
-rwxrwx--- 1 root everybody  98912 Apr 23 15:01 collection.anki2-wal
drwxrwx--- 2 root everybody 131072 Apr 23 15:04 collection.media
-rwxrwx--- 1 root everybody  24576 Apr 23 15:01 collection.media.db

while in my desktop computer, the file is called collection.media.db2.

ls -la ~/.local/share/Anki2/Experiments/
total 1012
drwxrwxr-x  4 rodrigo rodrigo   4096 Mar 26 13:49 .
drwxr-xr-x 18 rodrigo rodrigo   4096 Apr 23 15:27 ..
drwxrwxr-x  2 rodrigo rodrigo   4096 Mar 26 13:49 backups
-rw-r--r--  1 rodrigo rodrigo 991232 Mar 26 13:49 collection.anki2
drwxrwxr-x  2 rodrigo rodrigo   4096 Jul 12  2024 collection.media
-rw-r--r--  1 rodrigo rodrigo  24576 Mar 17 14:35 collection.media.db2
-rw-rw-r--  1 rodrigo rodrigo   3803 Jul 17  2024 deleted.txt

An edited file not syncing is a fairly rare occurrence though, isn’t it? Or is your workflow unique in a way that makes this more common? I question this as someone who frequently edits the audio files on my cards, but since I also regularly add/delete/clean-up my media too, I don’t think I’ve ever been caught without an updated file.

In my use case, it is not a rare ocurrence. I’m using Javascript which are stored in *.js files which I store in collection.media. The Javascript code is used in my flashcards to create some HTML elements. I regularly edit these Javascript files in my desktop computer using an external text editor. I want the Javascript code that is run in my desktop to be the same in AnkiDroid.

My final decision: I only add media using Anki Desktop, so for the time being I will disable sync in AnkiDroid and I will give read-only permissions to collection.media to Syncthing in my phone, so that my phone has an exact copy of collection.media. I will give read-only permission in my phone, just in case something problematic happens in AnkiDroid due to this hack and I lose all my files in collection.media in AnkiDroid.

I assume that having an outdated collection.media.db in my Android phone won’t be a huge problem, but we’ll see. I’ll be sharing my experiences in a few months.

If any of you have another idea for having an exact copy of collection.media between Anki Desktop and AnkiDroid (preferably without much manual work), please let me know.

That’s the way to get round the storage restrictions, which are a Google policy, so they only apply to the Play-Store version.

That – along with having different date/timestamps on different devices-- might suggest they are being created separately on each device. But again – I really don’t know much about that one.

Good luck on your trial run!

UPDATE: My initial plan was to host a Syncthing server in my desktop computer and assign read-only permissions to Syncthing in my Android device, so that I could have the exact same files in my Android device and in my desktop computer. Pros: In the scenario that anything goes wrong in my phone and the media files get deleted, the files wouldn’t be deleted in my desktop computer because I only have read-only permissions in Syncthing. Cons: I can’t add media files through AnkiDroid.

Regarding my initial plan, I don’t like the fact that I can’t upload files using my Android device, because I only have read-only permissions in Syncthing. In order not to have this restriction, I am currently using rsync in my desktop computer to send the media files which were edited to my Android device.