you're looping 'for account in qry'? Why not just do the query again but with a .delete vs a .all?

So I'd do something like


    qry = User.query.join(User.activation).\
 +            options(contains_eager(User.activation)).\
 +            filter(User.id == Activation.id).\
 +            filter(User.activated == False).\
 +            filter(User.last_login == None).\
 +            filter(Activation.valid_until < test_date)

    if not delete:
        return qry.all()
    else:
        qry.delete()