0
votes

I have a Google DataStore that I inherited and this Datastore has about 9M records. I'd like to remove all old records (say everything that is older than 1 month old). Generally speaking, when I use google cloud console Datastore->query by kind, I'm able to put SQL like statements:

select * from table limit 5

however when I try using the "query by gql" to do something like delete rows:

delete from table where date<"2019-01-01" 

It doesn't work- I get the error: "GQL Query error: Encountered "delete" at line 1, column 1. Was expecting: "select" ... "

Is there a simple way to run delete on all old records?

3
The easy way would be to write a script checking all conditions and then deleting.skaul05

3 Answers

0
votes

You can not use GQL to delete. If you want to delete you should use one of the client libraries.

0
votes

A simple way would be to write a script and run it whenever you need.

Code (Python 2.7):

from google.appengine.ext import ndb
from datetime import date

kind = ModelX  #kind whose entries you want to delete
check = date(2019,1,1)   # Date to be used to check records 

# GQL Query to fetch keys of all older records
all_keys = kind.gql("WHERE date <= :1",check).fetch(keys_only = True)

# Deleting all entries at once
ndb.delete_multi(all_keys)

Hope this answers your question!!!

0
votes

You can not use GQL to delete entities.

You can delete entities manually using UI delete button or using datastore.delete() method to delete entities