Thank you for reaching out to Microsoft Q & A forum
This behavior is likely due to eventual consistency or replication latency within Azure Cosmos DB. When the stored procedure is first read and returns a 404 (Not Found), it might already exist but hasn't yet propagated across all nodes. Attempting to create it then results in a 409 (Conflict), and trying to immediately replace it may again lead to a 404, as the system hasn’t fully registered it yet.
A practical approach is to introduce a short delay before attempting to replace the stored procedure:
await Task.Delay(1000); // wait 1 second
return await container.Scripts.ReplaceStoredProcedureAsync(...);
This gives Cosmos DB enough time to fully propagate the stored procedure metadata. Also, ensure that no other process is creating or modifying the same stored procedure simultaneously.
If you have found the answer provided to be helpful, please click on the "Accept answer/Upvote" button so that it is useful for other members in the Microsoft Q&A community.