如何修改Google Datastore中的Kind名称或删除Kind?
Hey there! Let me break down how to rename or delete a Kind in Google Datastore—since there’s no built-in one-click option for either, we’ll use practical workarounds that get the job done.
Datastore doesn’t support renaming a Kind directly because the Kind name is a core attribute of every entity. Instead, you’ll need to migrate your data to a new Kind:
Step 1: Export all entities from the original Kind
Use the Cloud SDK or Cloud Console to export your data. For the gcloud command:gcloud datastore export --kinds="OLD_KIND_NAME" gs://your-cloud-storage-bucket/path/This creates a backup of all entities from
OLD_KIND_NAMEin your Cloud Storage bucket.Step 2: Update the exported metadata to rename the Kind
Navigate to your Cloud Storage bucket and locate the exported files. You’ll need to:- Replace every instance of
OLD_KIND_NAMEwithNEW_KIND_NAMEin theindex.yamlfile (if you have indexes configured for the Kind). - Rename the folder structure under
namespace/fromOLD_KIND_NAMEtoNEW_KIND_NAME(this is where the entity data is stored).
- Replace every instance of
Step 3: Import the modified data back to Datastore
Run the import command to bring the renamed entities into Datastore:gcloud datastore import gs://your-cloud-storage-bucket/path/[EXPORT_FOLDER]/Make sure to use the correct path to the exported folder (it’ll have a timestamp in the name).
Step 4: Verify and clean up
Check the Cloud Console to confirm the new Kind has all your data. Once you’re sure everything’s migrated, delete all entities from the original Kind (follow the steps in the next section) to effectively remove it.
Datastore doesn’t have a "delete Kind" button—Kinds are dynamic, meaning they disappear automatically once there are no entities left under them. Here’s how to clear out all entities:
For small datasets (few hundred entities)
Head to the Cloud Console > Datastore > Entities, select the Kind you want to delete, check the box at the top to select all entities, then click the delete button. This works great for small volumes but isn’t efficient for large datasets.For large datasets
Use a client library or script to batch-delete entities. Here’s a quick Python example using the Google Cloud Datastore SDK:from google.cloud import datastore def delete_kind(kind_name): client = datastore.Client() query = client.query(kind=kind_name) query.keys_only() # Fetch only keys to save bandwidth # Batch delete in chunks (Datastore limits batch size to 500) keys = [] for key in query.fetch(): keys.append(key) if len(keys) == 500: client.delete_multi(keys) keys = [] if keys: client.delete_multi(keys) delete_kind("KIND_TO_DELETE")You can also use the
gcloud datastore deletecommand with a filter, but scripting gives you more control over large batches.Pro tip
If you want to back up the Kind before deleting it, run the export command from the renaming section first—better safe than sorry!
内容的提问来源于stack exchange,提问作者Marcelo Boeira

