Skip to content

Instantly share code, notes, and snippets.

Last active Jul 28, 2020
What would you like to do?
Anki 2.0 addon. Prevents learning (and re-learning) cards from being removed from a filtered deck. This allows you to rebuild the deck while keeping the learning cards inside of it; bypassing the problems with Anki decks where learning cards are set back to 'new' or 'review' when they exit a filtered deck.
# Filter Safety
# Anki 2.0 addon
# Author EJS
# Prevents cards in learning state from being moved out of a filtered deck
# when you rebuild it.
from anki.sched import Scheduler
#from aqt.utils import showInfo
def safeEmptyDyn(self, did, lim=None):
if not lim:
lim = "did = %s and type != 1 and queue != 1" % did
lim = "%s and type != 1 and queue != 1" % lim
ret = origEmptyDyn(self, did, lim)
return ret
origEmptyDyn = Scheduler.emptyDyn
Scheduler.emptyDyn = safeEmptyDyn
Copy link

eshapard commented Aug 16, 2017

This has not been extensively tested, but I think it works. Back up your collection first as a precaution.

Copy link

gajewsk2 commented Aug 18, 2017

Wow, this is timely. I was just whining about this issue the other day but hadn't had time to do something similar.

My decks relies on hierarchy, so eventually I need to figure out a way to get root decks to ignore the alphabetical order of subdecks and treat all the cards in the subdeck as a single deck, (to give me ascending ivl for all subdecks regardless of deck hierarchy). This might be a nice stop-gap until I figure that out and stop using filtered decks for anything besides cramming.

Thanks for making it public.

UPDATE: been using since i commented. Has been great.

Copy link

EliCodesForFun commented Oct 24, 2018

Godlike. I've been using filtered decks for years, but I just used the workaround by including a "is:due -is:learn" but it's not a perfect solution for cards that are in the re-learning queue in the filtered deck, which can get lost if you have to rebuild. Awesome fix!

Copy link

ArtappsLaurens commented Dec 4, 2019

Do you have any plans of porting this add on to 2.1? Would love that. Keep up the good work

Copy link

Geniusssmit commented Dec 4, 2019

Still waiting for 2.1 :)

Copy link

thiswillbeyourgithub commented Dec 24, 2019

Waiting for 2.1 port too :)

Copy link

eshapard commented Feb 24, 2020

Sorry, I don't use Anki anymore, so I'll probably never get around to porting it to 2.1.

If anyone wants to take a crack at it, it might work if you rename it to and drop it in a subdirectory under the 2.1 addons directory.


Copy link

albo2albo commented Jul 26, 2020

I've just tried this under 2.1 and it appears to work, by more or less following eshapard's comment above on 24th Feb. Here's what I did:

  1. create a folder called filterSafety in the addons folder ie AppData\Roaming\Anki2\addons21\filterSafety
  2. in this folder create a file called which contains lines 1 - 20 from above.
  3. restart Anki

You should note that there are two underscores before and after 'init', as seen in point 2 above.

Hope this helps someone else.


Copy link

thiswillbeyourgithub commented Jul 28, 2020

Am trying this too. Thanks for the quick tutorial @albo2albo !

If I don't come back here to complain then it probably works OR seems to work OR didn't cause major harm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment