0

I am working on Azure Cosmos DB with SQL Api. I am using Azure SDK from:

<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure-documentdb</artifactId>
    <version>2.4.7</version>
</dependency>

in order to delete an Item (document) from collection, I am using:

        String documentLink = collectionLink + "/docs/" + documentId;
        RequestOptions options = new RequestOptions();
        options.setPartitionKey(new PartitionKey(String.valueOf(documentId)));
        documentClient.deleteDocument(documentLink, options);

When the desired document exists, then this code works perfectly fine. When the document with documentId does not exist then I am getting an Exception:

com.microsoft.azure.documentdb.DocumentClientException: Entity with the specified id does not exist in the system.

Is there any way to delete documents "silently" - meaning no Exception will be thrown when document does not exist?

fascynacja
  • 1,625
  • 4
  • 17
  • 35

2 Answers2

2

Trying to delete a non-existent item is certainly illegal.Such request can't be tolerated in cosmos db(No method like DeleteIfExist). You also could find the 404 http status code from Cosmos DB REST API.

So,you have to capture this exception use Try-Catch or Throw to deal with it.

try {
            RequestOptions options = new RequestOptions();
            options.setPartitionKey(new PartitionKey(DOCUMENT_ID));
            documentClient.deleteDocument(documentLink, options);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getLocalizedMessage());
        }

enter image description here

Jay Gong
  • 23,163
  • 2
  • 27
  • 32
  • I could do that. Lets say it would be my "expected" scenario that sometimes the document does not exist. But how to distinguish from real exception scenario? Timeouts, connection issues, or other db related problems (wrong partition key etc.) ? – fascynacja Mar 12 '20 at 09:40
  • @fascynacja All error details are contained in the exception message.You could capture that and analysis it. – Jay Gong Mar 13 '20 at 08:28
  • I would prefere to not base my logic on if(e.getLocalmizedMessage().contains("some string")). Or you mean something else? – fascynacja Mar 13 '20 at 09:06
  • @fascynacja Yeah, that's what i mean. Um....it seems that you want sdk shows type of exception automatically. I'm afraid that it is not supported in cosmos db sdk so far. Only thing i can say that maybe the http code could offer some assistance for you. – Jay Gong Mar 13 '20 at 09:39
  • @fascynacja Hi,if my answer is helpful for you,would please accept it to end this? Thx. – Jay Gong Mar 17 '20 at 09:26
  • it is helpful so i gave it a vote up. But it does not solve my issue in the way I want :) I hope someone else will add another answer – fascynacja Mar 17 '20 at 11:40
0

Based on @Jay Gong answer finally I have designed my code like that:

try {
      RequestOptions options = new RequestOptions();
      options.setPartitionKey(new PartitionKey(id);
      documentClient.deleteDocument(documentLink(id), options);
    } catch (DocumentClientException dce) {
        String code = dce.getError().getCode();
        if ("NotFound".equals(code)) {
            log.warn("Problem while deleting document with id [{}] from Azure Cosmos. error: [{}]", id, dce.getError());
        }
        else { //handle any other document exception code
        }
    } 
fascynacja
  • 1,625
  • 4
  • 17
  • 35