Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete Ancestor Tree in Google Cloud Datastore

How do I delete an entire ancestor tree via the API (Python protocol buffer) in Google Cloud Datastore?

For example, if I have entities stored in this structure: grandparent/parent/child, how can I delete the grandparent, and in so doing delete all the children and grandchildren of that "node"?

If I submit a delete request on the grandparent's key, the grandparent entity is deleted, but its children and grandchildren remain, and their path is still grandparent/parent/child, even though the grandparent entity was deleted.

like image 555
kyhjrf Avatar asked Sep 18 '14 03:09

kyhjrf


People also ask

How do I delete all entities in Datastore?

Open "Datastore Admin" for your application and enable Admin. Then all of your entities will be listed with check boxes. You can simply select the unwanted entites and delete them.

How do I delete an index in Datastore?

Deleting unused indexes When you are sure that old indexes are no longer needed, you can delete them by using the datastore indexes cleanup command. This command deletes all indexes for the production Datastore mode instance that are not mentioned in the local version of index.

How do I delete a GCP Datastore?

Unfortunately, there is no way to purge the previous existence of a Cloud Datastore database to try either Cloud Firestore in native or Datastore mode. You'll have to use a new project to try Cloud Firestore in either native or Datastore mode. @Eduardo Your answer should be considered the correct one.

How do I delete folders in Google cloud?

Navigate to the objects, which may be located in a folder. Click the checkbox for each object you want to delete. You can also click the checkbox for folders, which will delete all objects contained in that folder. Click the Delete button.


Video Answer


1 Answers

Deleting a parent entity will not delete any of the child entities. However, you can use an ancestor query to find the keys of all of the child entities and delete them as part of a single transaction. The steps would be:

  1. Begin transaction.
  2. Run a keys-only kindless ancestor query on the parent entity's key.
  3. Add each of the keys returned by #2 to the list of keys to delete.
  4. Commit the transaction.

Here's a partial code snippet:

# Create a transactional RunQueryRequest.
req = datastore.RunQueryRequest()
req.read_options.transaction = txn  # From previous BeginTransactionRequest.

query = req.query

# Add an ancestor filter.
key_filter = query.filter.property_filter
key_filter.property.name = '__key__'
key_filter.operator = datastore.PropertyFilter.HAS_ANCESTOR
path_element = key_filter.value.key_value.path_element.add()
path_element.kind = 'ParentKind'
path_element.name = 'parent-name'

# Make it a keys-only query.
query.projection.add().property.name = '__key__'

batch = self.datastore.run_query(req)
for entity_result in batch:
  # Add entity_result.entity.key to CommitRequest...
like image 183
Ed Davisson Avatar answered Dec 17 '22 11:12

Ed Davisson