'Clear all the queues from before a given position'

def _clear_queue_before_pos(self, position_to_delete):

    with Measure(self.clock, 'send_queue._clear'):
        keys = self.presence_changed.keys()
        i = keys.bisect_left(position_to_delete)
        for key in keys[:i]:
            del self.presence_changed[key]
        user_ids = set((user_id for uids in self.presence_changed.itervalues() for user_id in uids))
        to_del = [user_id for user_id in self.presence_map if (user_id not in user_ids)]
        for user_id in to_del:
            del self.presence_map[user_id]
        keys = self.keyed_edu_changed.keys()
        i = keys.bisect_left(position_to_delete)
        for key in keys[:i]:
            del self.keyed_edu_changed[key]
        live_keys = set()
        for edu_key in self.keyed_edu_changed.values():
            live_keys.add(edu_key)
        to_del = [edu_key for edu_key in self.keyed_edu if (edu_key not in live_keys)]
        for edu_key in to_del:
            del self.keyed_edu[edu_key]
        keys = self.edus.keys()
        i = keys.bisect_left(position_to_delete)
        for key in keys[:i]:
            del self.edus[key]
        keys = self.failures.keys()
        i = keys.bisect_left(position_to_delete)
        for key in keys[:i]:
            del self.failures[key]
        keys = self.device_messages.keys()
        i = keys.bisect_left(position_to_delete)
        for key in keys[:i]:
            del self.device_messages[key]
