How to delete data on app engine

cloudCMS has Freemarker directive to delete data from App Engine's datastore.

<@deleteEntity /> Directive

Delete using ID (Long) :
<@deleteEntity kind="<EntityName>" id=<LongID> />
Delete using Key( WebSafe String) :
<@deleteEntity kind="<EntityName>" key="<keyAsString>" />

Delete more than one row (all rows)

Delete all rows using offset and limit :
<@deleteEntity kind="<EntityName>" all=true limit=25 offset=10 ; keys />

Delete 25 rows starting from offset 10 and store the affected Keys in "keys" variable.

Why "limit" and "offset" with delete ?
The "limit" and "offset" feature is there to support delete operation without causing timeout issues. The app engine is shared resource and has time limitation on every operation. Using limit and offset application can build "delete all" feature for large dataset.

Delete more than one row (using Entity Property Filter)

Using Entity Filter, you can define which row(s) should be deleted for given Entity.

For more information on Filter see here.

<@filterEntity name="_f1" propertyName="age" operator="LESS_THAN" value=10 />

<@deleteEntity kind="<EntityName>" all="true" filter=_f1 limit=25 offset=10 />

Using multiple filters,

<@filterEntity name="_f1" propertyName="age" operator="LESS_THAN" value=50 />

<@filterEntity name="_f2" propertyName="age" operator="GREATER_THAN" value=10 />

<@deleteEntity kind="<EntityName>" all=true filter=[_f1,_f2] limit=100 />


Note : Multiple Filters requires a proper datastore-index on given Entity.

Delete session data

App Engine stores user session into _ah_SESSION Entity. Over the period it can grow very huge, use following code to delete old sessions.

 Deleted :
<@deleteEntity kind="_ah_SESSION" all=true  ; Keys>
${Keys?size}
</@deleteEntity>

blog comments powered by Disqus

Last Modified : Jul 10, 2009 2:15:21 PM