How to remove multiple objects from a GAE datastore using keys

I am using the Google App Engine on localhost. I have 2000 objects of type Book in a data warehouse. I want to delete the first 1900 (key range 1 to 1901). How can I do this from the interactive console? I use ndb as opposed to db

Perhaps there is some kind of range functionality.

For example, I try to do the following, but nothing happens.

 from myddb import Book list= Book.gql("WHERE ID < 193") for entity in list: db.delete(entity) 

EDIT:

Based on answer from @Lipis works

 from myddb import Book from google.appengine.ext import ndb book_keys = Book.query().fetch(keys_only=True) ndb.delete_multi(book_keys) 

But it all removes. I need to work with Key aka ID request as

 book_keys = Book.query(Article._Key < 1901).fetch(keys_only=True) 
+4
source share
3 answers

You should use ndb.delete_multi() :

 from google.appengine.ext import ndb book_keys = Book.query().fetch(keys_only=True) ndb.delete_multi(book_keys) 

You should browse the NDB Queries to see what other options you have and what you can achieve.

+10
source

EDIT

I have not tested the solution below, but I check it and let you know.

Also, this should greatly help the ndb cheat sheet

 q = Book.query(default_options=QueryOptions(keys_only=True)) if Book.ID < 1901: ndb.delete_multi([m.key for m in q.fetch(1900)]) 
+2
source

In ndb you use q = Book.query('query').fetch('number')

Then repeat and delete.

+1
source

All Articles