title: StatusCode: InvalidPartition when creating Collection then writing, reading, finally deleting collection. Happens on the second time do this sequence
body: <p>I have a REST API server that triggers some operations on my NoSQL Cosmos database</p>
  
  <p>My script has this pattern:
  1. create collection
  2. write 20 documents to the new collection
  3. query the collection (and verify query)
  4. delete the collection</p>
  
  <p>I boot up my REST server and make the API calls to do the 4 steps above. Everything works fine the first time. While the same server is running, when I try to do the 4 steps above a second time I get this error when doing the querying part (step 3):</p>
  
  <blockquote>
    <p>com.microsoft.azure.documentdb.DocumentClientException: Message:
    {"Errors":["The requested resource is no longer available at the
    server."]} ActivityId: ba46f15a-3615-4764-adea-419c17d26c8e, Request
    URI:
    /apps/91dbada6-1498-4f1b-b8ae-047db532568d/services/66113bcb-793b-4d5b-ab9d-6ad5738cda30/partitions/0c2487d2-a50c-4ea5-9859-b61335b06c25/replicas/132049092719729780s/,
    RequestStats:  RequestStartTime: 2019-06-14T18:50:41.0726964Z,
    RequestEndTime: 2019-06-14T18:50:41.0726964Z, Number of regions
    attempted: 1 ResponseTime: 2019-06-14T18:50:41.0726964Z, StoreResult:
    StorePhysicalAddress:
    rntbd://cdb-ms-prod-eastus1-fd28.documents.azure.com:16711/apps/91dbada6-1498-4f1b-b8ae-047db532568d/services/66113bcb-793b-4d5b-ab9d-6ad5738cda30/partitions/0c2487d2-a50c-4ea5-9859-b61335b06c25/replicas/132049092719729780s/,
    LSN: 25, GlobalCommittedLsn: 24, PartitionKeyRangeId: , IsValid: True,
    StatusCode: 410, SubStatusCode: 1000, RequestCharge: 0, ItemLSN: -1,
    SessionToken: -1#21, UsingLocalLSN: True, TransportException: null,
    ResourceType: Document, OperationType: Query , SDK:
    Microsoft.Azure.Documents.Common/2.4.0.0, StatusCode: InvalidPartition</p>
  </blockquote>
  
  <p>'''</p>
  
  <pre><code>public void createDocumentCollectionIfNotExists(String databaseName, String collectionName) throws Exception {
      String databaseLink = String.format("/dbs/%s", databaseName);
      String collectionLink = String.format("/dbs/%s/colls/%s", databaseName, collectionName);
  
      try {
          this.client.readCollection(collectionLink, null);
          System.out.println(String.format("Found %s", collectionName));
      } catch (DocumentClientException de) {
          if (de.getStatusCode() == 404) {
              DocumentCollection collectionInfo = new DocumentCollection();
              collectionInfo.setId(collectionName);
  
              RequestOptions requestOptions = new RequestOptions();
              requestOptions.setOfferThroughput(400);
  
              this.client.createCollection(databaseLink, collectionInfo, requestOptions);
          } else {
              throw de;
          }
      }
  }
  </code></pre>
  
  <p>'''</p>
  
  <pre><code>public String queryCollection(String databaseName, String collectionName) {
      FeedOptions queryOptions = new FeedOptions();
      queryOptions.setEnableCrossPartitionQuery(true);
      queryOptions.setMaxDegreeOfParallelism(-1);
  
      String collectionLink = String.format("/dbs/%s/colls/%s", databaseName, collectionName);
      System.out.println(collectionLink);
  
      Iterable&lt;Document&gt; queryResults = client.queryDocuments(
              collectionLink,
              String.format(baseQuery, collectionName),
              queryOptions).getQueryIterable();
  
      return buildJsonList(queryResults);
  }
  </code></pre>
  
  <p>'''</p>
  
  <pre><code>public int createDocument(String databaseName, String collectionName, EventDocument document) throws DocumentClientException {
      System.out.println("trying to write to database");
      String collectionLink = String.format("/dbs/%s/colls/%s", databaseName, collectionName);
  
      ResourceResponse&lt;Document&gt; response = client.createDocument(collectionLink, document, new RequestOptions(), false);
  
      return response.getStatusCode();
  }
  </code></pre>