Unable to Delete bulk records from Couchbase

We are trying to remove records from Couchbase using async().remove(key) where key is the document to be removed.

We are sending a list of keys using Observable.from(documents).flatMap(new Func1<String, Observable<JsonDocument>>(). The list contains some values which are not present in Couchbase Bucket. For that purpose we added onErrorResumeNext(Observable.<JsonDocument> empty()).last().toBlocking().singleOrDefault(null).

However, when the remove function encounters a key not in Couchbase, it throws an Exception stating NoSuchElementException:Sequence Contains No Elements. Even after using onErrorResumeNext, we are getting the exception. What could be the cause ?

That’s because of the last() operator. You should:

  1. Perform both bucket.async().get(k) and onErrorResumeNext(Observable.empty()) inside the flatMap, but nothing more
  2. Just after the flatMap, simply do toBlocking().lastOrDefault(null).

This way you won’t get the exception and will receive the last document removed (should match the last key in the collection), or null only if the collection is empty or every key didn’t map to a stored document.