Randomizes the order of new (unseen) cards in an Anki .apkg deck file.
Anki .apkg files are ZIP archives containing a SQLite database
(collection.anki2 or collection.anki21). Each card has a due field
that controls its position in the new-card queue. This script:
- Extracts the
.apkgto a temporary directory - Opens the SQLite collection database
- Finds every card with
queue = 0(new / unseen cards) - Assigns them a freshly shuffled set of position numbers
- Repacks the archive
Only new cards are affected — cards already in review or relearning are left untouched.
- Python 3.10+
- No third-party packages (uses only stdlib:
sqlite3,zipfile,json,random)
# Clone the repo
git clone https://github.com/your-username/anki_reshuffle.git
cd anki_reshuffle
# Create and activate a virtual environment
python3 -m venv .venv
source .venv/bin/activate # macOS / Linux
# .venv\Scripts\activate # Windows
# To deactivate when done:
deactivate# Reshuffle all new cards (overwrites input, saves a .bak backup)
python reshuffle.py MyDeck.apkg
# Write reshuffled deck to a new file
python reshuffle.py MyDeck.apkg MyDeck_reshuffled.apkg
# Reshuffle only a specific deck (sub-decks are included automatically)
python reshuffle.py MyDeck.apkg --deck "Japanese::Vocabulary"
# Reproducible shuffle with a fixed seed
python reshuffle.py MyDeck.apkg --seed 42
# List all deck names inside an .apkg
python reshuffle.py MyDeck.apkg --list-decks| Flag | Description |
|---|---|
output |
Optional output path. Omit to overwrite the input (a .bak is created). |
--deck DECK_NAME |
Limit reshuffling to one deck and its sub-decks. |
--seed SEED |
Integer seed for a reproducible shuffle. |
--list-decks |
Print all deck names in the .apkg and exit. |
Import the reshuffled deck into Anki via File > Import. If you reshuffled
in place (no output argument), your original deck is preserved as
MyDeck.apkg.bak.