In Google App Engine using NDB, how can I completely remove the full namespace?
The following code deletes all entities:
def delete(namespace): namespace_manager.set_namespace(namespace) for kind in ndb.metadata.get_kinds(): keys = [ k for k in ndb.Query(kind=kind).iter(keys_only=True) ] ndb.delete_multi( keys )
However, on the dev server, the namespace still exists when called:
ndb.metadata.get_namespaces()
and during production, exceptions arise when trying to delete a system view, for example:
illegal key.path.element.type: __Stat_Ns_Kind__
How to completely exclude a namespace?
As @jeremydw pointed out, namespace information is stored as __namespace__
. However, this does not behave as a normal view, and, in particular, deleting objects has no effect:
id_namepace = 'some_test' print list( ndb.Query(kind='__namespace__') ) # id_namespace is not present # SomeModel is some existing model key_entity = ndb.Key('SomeModel', 'some_string_id', namespace=id_namepace) entity = datastore.CustomerAction(key=key_entity) entity.put() print list( ndb.Query(kind='__namespace__') ) # id_namespace is present (expected, namespace was implicitly created by adding one entity in it) key_entity.delete() print list( ndb.Query(kind='__namespace__') ) # id_namespace is present (expected, namespace still exists but contains no entities) key_namespace = ndb.metadata.Namespace.key_for_namespace(id_namepace) key_namespace.delete() print list( ndb.Query(kind='__namespace__') ) # id_namespace is still present (not expected, kind='__namespace__' does not behave as a normal kind)
source share