def METHOD_NAME(datetime_to_delete_from, query_filter):
    query_limit = 10000
    subquery = (
        db.session.query(InboundSms.id)
        .filter(InboundSms.created_at < datetime_to_delete_from, *query_filter)
        .limit(query_limit)
        .subquery()
    )
    deleted = 0
    # set to nonzero just to enter the loop
    number_deleted = 1
    while number_deleted > 0:
        number_deleted = InboundSms.query.filter(InboundSms.id.in_(subquery)).delete(synchronize_session="fetch")
        deleted += number_deleted
    return deleted