'Args:
user_id(str): The recipient user_id.
device_id(str): The recipient device_id.
up_to_stream_id(int): Where to delete messages up to.
Returns:
A deferred that resolves to the number of messages deleted.'

@defer.inlineCallbacks
def delete_messages_for_device(self, user_id, device_id, up_to_stream_id):

    last_deleted_stream_id = self._last_device_delete_cache.get((user_id, device_id), None)
    if last_deleted_stream_id:
        has_changed = self._device_inbox_stream_cache.has_entity_changed(user_id, last_deleted_stream_id)
        if (not has_changed):
            defer.returnValue(0)
    def delete_messages_for_device_txn(txn):
        sql = 'DELETE    FROM    device_inbox    WHERE    user_id    =    ?    AND    device_id    =    ?    AND    stream_id    <=    ?'
        txn.execute(sql, (user_id, device_id, up_to_stream_id))
        return txn.rowcount
    count = (yield self.runInteraction('delete_messages_for_device', delete_messages_for_device_txn))
    last_deleted_stream_id = self._last_device_delete_cache.get((user_id, device_id), 0)
    self._last_device_delete_cache[(user_id, device_id)] = max(last_deleted_stream_id, up_to_stream_id)
    defer.returnValue(count)
