Cannot delete Cosmos DB Item with Illegal Id

Nicole Cortes 6 Reputation points
2021-10-22T17:46:33.143+00:00

143041-image.png

Three items in our Cosmos DB got created with an illegal id, we can Get the items via code but we are unable to update or delete to fix the issue. How can we get these out of our database?

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,455 questions
0 comments No comments
{count} vote

3 answers

Sort by: Most helpful
  1. Tim Connor 11 Reputation points
    2022-08-16T22:03:22.943+00:00

    You can delete the documents with invalid IDs server via a stored procedure which can be executed from the v3 SDK without downgrading the v2 SDK version e.g.:

    function deleteMyDoc(docId) {  
        var collection = getContext().getCollection();  
      
        var query =  
        {  
            'query' : 'SELECT * FROM c where c.id = @docId',  
            'parameters' : [{'name':'@docId', 'value': docId}]   
        };  
          
        if(!collection.queryDocuments(  
            collection.getSelfLink(),  
            query,  
            function (err, docs, options) {  
                if (err) throw err;  
                if (!docs || docs.length !== 1) {  
                    var response = getContext().getResponse();  
                    response.setBody('Document Not Found');  
                }  
                else {  
                    var suppression = docs[0];  
                    if(!collection.deleteDocument(suppression._self, {etag: suppression.etag})) {  
                        throw new Error('Delete was not accepted by the server.');  
                    }  
      
                    var response = getContext().getResponse();  
                        response.setBody("Document Deleted");  
                    }  
                }  
            )){  
                 throw new Error('The query was not accepted by the server.');  
            }  
        }  
    

    An then execute from c#

    var invalidId = "grabage/junk";  
    await container.Scripts.ExecuteStoredProcedureAsync<string>("deleteMyDoc", new PartitionKey("my partition key"), new[] { invalidId });  
    
    
    
    
    
    
    
      
      
    
    2 people found this answer helpful.

  2. Oury Ba-MSFT 16,636 Reputation points Microsoft Employee
    2021-10-22T20:05:11.587+00:00

    Hi @Nicole Cortes Thank you for posting your Question on Microsoft Q&A.

    Resource Id should not contain the properties - '/', '\', '?', '#'

    https://learn.microsoft.com/en-us/dotnet/api/microsoft.azure.documents.resource.id?view=azure-dotnet

    Invalid character in an item ID
    An item is inserted into Azure Cosmos DB with an invalid character in the item ID.

    Solution:
    Change the ID to a different value that doesn't contain the special characters. If changing the ID isn't an option, you can Base64 encode the ID to escape the special characters. Base64 can still produce a name with a invalid character '/' which needs to be replaced.

    Items already inserted in the container for the ID can be replaced by using RID values instead of name-based references.

    142969-image.png

    Please let me know if that help. If not we can discuss further on possible solutions.

    Regards,
    Oury

    1 person found this answer helpful.

  3. Josh Noe 21 Reputation points
    2022-01-20T20:13:07.82+00:00

    Copied from this SO answer: https://stackoverflow.com/a/67268470/732673

    1. Set the TTL property on the container to "On" (default is Off). What this setting mean is that documents in the container will automatically delete if TTL is specified on the document and once the TTL has expired. This you can set through Data Explorer in Azure Portal as shown below:

    166932-image.png

    1. Update the document and set the TTL property on the document. You can do so by using Cosmos DB SDK. See sample code where it's set to 30 seconds (you can set it as low as 1 second). Once the upsert operation succeeds, you will see the document gets automatically deleted after TTL has expired on the document (30 seconds in my case). static async Task UpdateItemTtl()
      {
      CosmosClient cosmosClient = new CosmosClient(connectionString);
      Container container = cosmosClient.GetContainer(databaseName, containerName);
      string documentBody = "{\"ttl\": 30, \"Code\": \"https://CokeURL.com/gbm330\", \"Quantity\": 1, \"ActivityId\": \"0000\", \"id\": \"https://CokeURL.com/gbm330\"}";
      Object o = JsonConvert.DeserializeObject(documentBody);
      ItemResponse<object> response = await container.UpsertItemAsync(o, new PartitionKey("0000"));
      documentBody = JsonConvert.SerializeObject(response.Resource);
      Console.WriteLine(documentBody);
      }

    This still requires some code unfortunately because of the bug in Azure Portal where you can't access or delete the docs.

    0 comments No comments