Search code examples
google-app-maker

Deleting all entered data in a datastore in AppMaker


I'm testing a project with two databases joined by a MANY-ONE relation (Devices -> Employees). Both datasources are Google Drive Tables. How can I delete all of the data in both of these tables, without deleting the tables themselves? I'd like to keep their metadata and relations intact but start with fresh data. Based on another answer, I've tried to run the following commands on button press:

var records = app.models.Device.newQuery().run(); 
app.deleteRecords(records);
var records = app.models.Employee.newQuery().run(); 
app.deleteRecords(records);

But I receive the error:

app.models.Device.newQuery is not a function at AddDevice.Button1.onClick:1:33

And I wasn't sure where to go from there. Thanks in advance.


Solution

  • First of all, you should run this code on server side:

    // server script
    function deleteAll() {
      var records = app.models.Employee.newQuery().run(); 
      app.deleteRecords(records);
      ...
    }
    
    
    // Client script, for instance button's onClick event handler
    google.script.run.deleteAll();
    

    Then you can simplify your life, by specifying relation Owner. If you have it configured, then when you delete master record all related records will be deleted(cascade deletion). If you properly setup relations for all your models, then you'll need to delete records only from one model/table and all other records will be delete as well: Relation owner

    PS

    As a quick and simple workaround, you can simply create a brand-new deployment(out of the box all models/tables will be empty).