Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete deletion of cards after clicking on sync manually #14

Closed
BrianRonin opened this issue Sep 1, 2024 · 8 comments
Closed

Complete deletion of cards after clicking on sync manually #14

BrianRonin opened this issue Sep 1, 2024 · 8 comments
Assignees
Labels
bug Something isn't working

Comments

@BrianRonin
Copy link

BrianRonin commented Sep 1, 2024

After creating some new cards, and moving some from there to here and clicking to synchronize manually (I do this to avoid problems) it loaded and simply deleted all cards from my yanki in anki and changed all the IDs of my cards in obsidian, thus leaving my progress lost forever, even making a backup of my anki on my cell phone it does not recognize it and creates everything again because of the changed IDs in obsidian, thus making it not recognize the backup and create everything again.

Solution:
I don't know how you created this plugin, but a possible solution (at least for me it would be cool) would be to put the card's progress if it is for review or if it is new, anyway, leave this on Obsidian's side as the source of truth, but this would obviously create another problem: the person would study on their cell phone and when synchronizing with Obsidian they would lose their progress. So when synchronizing, get and download only all the card's progress to Obsidian and then upload all the changes along with the progress that already existed. Also create a button to do this by manually synchronizing the progress from Anki to Obsidian. This would at least avoid what happened to me of losing all the progress. I only have 80 cards, but imagine someone with 2k cards having to learn everything again because they lost their progress for some reason. It simply doesn't work. The best case scenario would be to have no error (but let's be honest, it's not hard for this shit to happen), but to have a way around it if it happens like what happened to me, simply delete EVERYTHING and change ALL IDs, thus making it irreversible. recovery, having a kind of backup ready in obsidian of the progress since the ensi cards are already the backup in obsidian the true source but the progress of the cards is missing, this is my suggestion, I know this would make it more complex but it would be ideal from my point of view.

Error reproduction:
I'm leaving a link below where I uploaded my Vault environment and also my Anki to simulate the error, just click on synchronize and the error should occur

https://drive.google.com/drive/folders/1oaGvJ84IQlM5gQ-3vQ03AJ0fwMxZaqow?usp=sharing

While we don't know what happened and the problem is not solved, if you know what could have been the cause so I can avoid doing so this problem doesn't happen again I would appreciate it.

@kitschpatrol
Copy link
Owner

kitschpatrol commented Sep 2, 2024

Hi Brian, sorry this happened — I'll investigate with the attachment you provided, but four quick points first:

  1. Anki should be making automatic backups, which should allow you to restore — including your study history.

  2. Can I confirm that all of the cards deleted were originally created with Yanki, and not directly in Anki or by other means? Yanki should only ever modify or delete cards it created. (If it is deleting other cards, that's an issue...)

  3. I looked into syncing study progress back to Obsidian from Anki, but it's too complex to use as a safe backup for the reasons you described.

  4. From your description of "moving some from there to here", my best guess is that you moved your flashcard notes outside the synced folder in Obsidian and then hit the Sync button... at which point you're effectively syncing an empty folder which, by design, would indeed delete all of your cards.

Assuming (for now) that this was an issue of accidentally moving cards outside the synced folder before a sync, it's still a shame that that happened even if it's technically not a bug... maybe popping up a confirmation dialog before card deletions would make sense.

I'll see if I can reproduce anything else with the sample vault you provided (thank you for that), and will update the issue.

@kitschpatrol
Copy link
Owner

kitschpatrol commented Sep 2, 2024

Thanks again for providing the vault and Anki package, very helpful.

I've done some testing with the files provided, and at least from the state provided things are working as expected, because the ID of the notes in the Anki deck do not match the ID of the notes in Obsidian (except for one note, a.md).

So, when the sync is requested, first deletes from Anki any notes it had previously created which do not have a local Obsidian note with a matching ID in its frontmatter. Likewise later in the sync, if a local note already has an ID in its frontmatter but no matching card is found in Anki, then the note is created in Anki and the frontmatter in the local Obsidian note is updated with the new ID. (Anki decides the values of the IDs, not Obsidian.)

Before sync:

Card ID in Obsidian ID in Anki Match
a.md 1725021762931 1725021762931
Números compostos.md 1725225051308 1724242705404
etc. etc. etc.

After sync:

Card ID in Obsidian ID in Anki Action
a.md 1725021762931 1725021762931 Unchanged
Números compostos.md 1725244609391 1725244609391 Recreated
etc. etc. etc. Recreated

After that, subsequent syncs also work as expected (with all cards unchanged) because the IDs in Obsidian and Anki match again.

So, technically, exactly what we'd expect to happen from the state provided in your test files is happening, but then the more important question becomes.... what happened before when "creating some new cards, and moving some from there to here" to create the deviation between Anki and Obsidian in the first place?

Were notes moved between vaults, possibly?

Was auto-sync enabled at some point?

Were cards temporarily moved out of the synced Anki folder?

Are you using multiple Anki profiles?

@kitschpatrol kitschpatrol self-assigned this Sep 2, 2024
@kitschpatrol
Copy link
Owner

Also looking into whether this has something to do with unicode characters in the deck names, related to #13.

@kitschpatrol
Copy link
Owner

Hi Brian, I investigated this further today and it seems like there is an edge case where the underlying Anki-Connect library that Yanki uses to communicate with Anki is reporting decks as being empty when they actually are not, which results in Yanki "cleaning up" decks it thinks are empty, deleting valid notes in the process. 😓

This seems to only occur only when there are special characters in the deck name, as you have in your Matemática etc. sample Vault folders. This was a case I did not have in my automated tests (until now).

So, mea culpa.

I think I have a fix, and have included it in the latest 1.4.3 update. So please give that a try and let me know if you see this issue again.

@kitschpatrol kitschpatrol added the bug Something isn't working label Sep 2, 2024
@BrianRonin
Copy link
Author

BrianRonin commented Sep 3, 2024

Hi, thank you very much for your attention, answering your questions:

Were notes moved between vaults, possibly?
R: No
Was auto-sync enabled at some point?
R: No
Were cards temporarily moved out of the synced Anki folder?
R: No
Are you using multiple Anki profiles?
R: No

And unfortunately it is not really possible to reproduce the error since the obsidian ids have been changed, but I will take the precaution of saving the yanki folder before synchronizing, so if the error occurs again (which I think you found the problem and it must be the characters) then I will send you a real reproduction of the problem.
I updated the plugin

@kitschpatrol
Copy link
Owner

I'm going to assume that this is fixed since the related tests are passing on my end and I haven't heard any updates to the contrary in this thread.

But please don't hesitate to reopen if you're still seeing this issue.

@BrianRonin
Copy link
Author

BrianRonin commented Dec 8, 2024

Today it happened again, but luckily I made a backup before and I found out what happened that day

I use Google Drive to sync my Obsidian vault folder, it must have been disconnected for a while and Obsidian simply disappeared with the vault, so I had to put it back, and there would be no problem if it weren't for a simple fact, every time you disconnect or disconnect the vault by itself, as was my case when using Google Drive, then you put it back in Obsidian it changes the "vault ID" and that's where the problem lies because it recreates everything again as if it were a new vault, but the biggest problem comes later, it changes all the IDs of the notes in Obsidian, leaving the progress unrecoverable, unless you have the backup before you clicked sync from Obsidian and Anki like I did, after recovering the backup from Obsidian and Anki I manually changed the ID of my vault in obsidian.json in AppData and I was able to recover my progress.

What I learned so that this doesn't happen again is basically to check if the vault ID matches the vault ID in Anki, because if it doesn't, it's game over, I'll lose all my progress and I already have almost 500 study cards, it's going to be painful.

What you could do would be to make it very clear in the plugin that disconnecting and connecting the vault (changes to the vault ID) will cause everything to be recreated again and will change the note IDs, making the progress unrecoverable.

@kitschpatrol
Copy link
Owner

Thanks for the update.

Your description tracks the expected behavior when the Vault ID changes — but I can't reproduce the part where the existing Anki notes created via a different vault are deleted from Anki after a Yanki sync.

When I go through the process you describe of syncing from one Obsidian vault and then recreating the same note in another Obsidian vault, I just end up with duplicate notes in Anki with different values in the YankiNamespace fields and different Note IDs, which is consistent with the designed behavior. (The original bug in this thread where notes really were deleted should have been fixed.)

Yanki is designed to never touch notes in Anki that don't match the the YankiNamespace used during a sync. Since your new vault will have a different YankiNamespace (derived from the Vault ID as you discovered) than the notes synced form the previous vault should not be deleted in Anki.

If the notes with the previous YankiNamespace really were deleted in Anki, let me know and please provide the exact steps to reproduce.

Regardless, I'm planning to make some changes to the namespace logic in a future version of Yanki to prevent this and also support some other synchronization cases:

  1. Generate the YankiNamespace from the Vault ID once on first sync, and then save it to the Plugin settings where it can be synced.
  2. Expose the namespace string in the plugin settings for advanced use cases / manual vault migrations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants