1

In Google Datastore (or Firestore), if you delete an entity, is it possible for it's entity id to be reused on a newly created entity in the future?

For example:

  1. I create an entity with an auto allocated id.
  2. I delete that entity.
  3. Sometime in the future I create a new entity with an auto allocated id.

Is it possible the new entity could be assigned an id that was previously used?

Micro
  • 10,303
  • 14
  • 82
  • 120

2 Answers2

1

Is it possible the new entity could be assigned an id that was previously used?

Yes, it's possible, but extremely unlikely. It's the same probability of any two IDs being randomly generated as equal values in any situation that you can imagine (which is to say, essentially impossible to collide). The IDs are not "reserved" in any way - they are just randomly generated by the SDK that created them. You could generated your own IDs as well and get the same effect.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • I am worried that the id of an entity I deleted might be used again by the system. But you say they are all randomly generated anyways. So it is safe to assume the id of a deleted entity will most likely not be used again in that entity? Since the probability of it is extremely unlikely? – Micro Dec 19 '21 at 20:56
  • From the answer above: "It's the same probability of any two IDs being randomly generated as equal values in any situation that you can imagine". The situation you just imagined is exactly the same. It's about as rational to worry about these random IDs colliding as it is worry about the earth colliding with the sun any time during your lifetime. – Doug Stevenson Dec 19 '21 at 21:56
  • For clarification, does the datastore system check for the existence of an Id when it is created automatically upon entity put and/or when using allocate id? Or does it just run some sort of algorithm to create the id without checking any existing ids? – Micro Nov 27 '22 at 02:43
0

It is possible, but you can avoid that by calling reserveIds after deletion.

max
  • 29,122
  • 12
  • 52
  • 79