anki-connect icon indicating copy to clipboard operation
anki-connect copied to clipboard

deleteDecks does not work on 2.1.44 due to changes to Anki's DeckManager rem function

Open baderj opened this issue 4 years ago • 2 comments

The latest release of Anki, 2.1.44, requires both cardsToo and childrenToo to be set to true when calling rem on a DeckManager:

def rem(self, did: int, cardsToo: bool = True, childrenToo: bool = True) -> None:
        "Remove the deck. If cardsToo, delete any cards inside."
        if isinstance(did, str):
            did = int(did)  
        assert cardsToo and childrenToo
        self.col._backend.remove_deck(did)

AnkiConnect passes down cardsToo, which will result in an AssertionError if cardsToo is set to false (which is also the default).

As a result, not only is the deck not deleted, the way AnkiConnect parses the exception means that the return will have an empty string in the error field {"error": ""}. This is different from the success case {"error": null}, but still not ideal. If the string representation of an exception is empty, then AnkiConnect should return a fallback string.

To fix the issue I would:

  • If the user set cardsToo to true, then perform the deletion and return a deprecation warning that cardsToo has no effect and will always be true in the future
  • If the user did not set cardsToo or set it to false, then don't delete the deck and just return an error stating that it is not possible to delete the deck without also removing cards.

Note that rem will also be deprecated in the next releases for remove, which no longer has any cardsToo parameter.

baderj avatar Jul 13 '21 08:07 baderj

For reference, here's the code to the new remove function: https://github.com/ankitects/anki/blob/1cdf0b1e9d86d3ac453118dfb59aa55b90630ece/pylib/anki/decks.py#L147

Does anyone know if calling remove will remove the cards? What about if it's a filtered deck? I don't understand this new code...

thiswillbeyourgithub avatar Sep 21 '21 20:09 thiswillbeyourgithub

Does anyone know if calling remove will remove the cards?

I don't understand the code neither but it seems to delete the files too.

self.decks().remove([did])

col = Collection("path/to/collection.anki2")
col.db.all("select count() from cards")
# [[0]]

adoussot avatar Nov 01 '21 16:11 adoussot