Can't figure out how to bulk delete/modify filtered or selected users firebase auth table

I'm trying to create a useful dashboard and my first challenge is getting rid of many of my test accounts etc.

i've thus far managed to make a table and identify those users (e.g. anonymous users or my many test emails). I can multiple select items and I can also run a search for a type and select all.

But deleting them still only works when a single user is selected.

I've spoken to support and seen some other articles in the community but either not exactly what i'm trying to do or it's still to advanced for me to see how to convert it to do what I want.

At the moment I have the following:

  • a query to list my firebase Auth user table.
  • A table to display it.
  • A delete user query.
  • A button to trigger deletion of the selected user.

I have found the following:

  • When the deleteUser query has the UID as the following it successfully passes the UID of the selected user and deletes it but only if it's a single item.

{{ userTable.selectedRow.data.uid }}

  • I can make an array of the selected UID by using the following:

{{ formatDataAsObject(userTable.selectedRow.data).uid }}

But then it doesn't run the delete as it's not a valid UID.

  • Support has advised me that firebase doesn't support this sort of thing so JS is the next step. They linked me a great article which I'm trying to parse. It does seem to be doing something, but not bulk deleting my files. Below is both the link and what I've written in a new js query.

https://docs.retool.com/docs/scripting-retool#promises-and-async-queries

var rows = [{ a: 1 }, { a: 2 }, { a: 3 }, { a: 4 }];

function runQuery(i) {
  if (i >= rows.length) {
    console.log("Finished running all queries");
    return;
  }
  var data = rows[i];
  console.log("Running query for row", data);

  DeleteUser.trigger({
    additionalScope: {
      data: data,
    },
    // You can use the argument to get the data with the onSuccess function
    onSuccess: function (data) {
      runQuery(i + 1);
    },
  });
}

runQuery(0);

Instead I'm receiving the error:

DeleteUser: {"status":422,"message":"There is no user record corresponding to the provided identifier.","statusCode":422,"error":"Unprocessable Entity","data":null,"queryExecutionMetadata":{"estimatedResponseSizeBytes":139,"resourceTimeTakenMs":191,"isPreview...

Hey @arkonis!

It seems as though the key here might be what you pass to the DeleteUser query as additionalScope. Since it looks like you only need the UID here can you try declaring rows as just an array of the UIDs in your table using one of the following?

var rows = formatDataAsObject(userTable.data).uid;
var rows = userTable.data.uid;

It may be the case that you need a different syntax here. If you can share a screenshot of some of your table data expanded in your debug console we can help grab the exact syntax you need.

Thank you for the help. I'm not certain if it's the same as the solution I found through other means but it's working now to my great joy.

I've posted my solution below for others who have the same issue as it is a common requirement for Firebase Auth users and has been a headache.

var rows = SelectedUserList.uid;
console.log(rows);
console.log("Beginning bulk user deletion. Please standby.");

/*const promises = SelectedUserList.uid(() => {
  return DeleteUser.trigger({
    additionalScope: {
    },
  });
});

return Promise.all(promises);
*/

var uids = SelectedUserList.uid;

function runQuery(i) {
  if (i >= uids.length) {
    console.log("Finished running all queries");
    return;
  }
  var uidarray = uids[i];
  console.log("Running deletion for user id:", uidarray);

  DeleteUser.trigger({
    additionalScope: {
      uidarray: uidarray,
    },
    // You can use the argument to get the data with the onSuccess function
    onSuccess: function (uidarray) {
      console.log("User" + uidarray + "has been deleted successfully");
      runQuery(i + 1);
    },
  });
}

runQuery(0);

And for anyone who stumbles here with as little pre-knowledge as myself,
the words 'rows', 'uids' and 'uidarray' are just names i gave to variables rather than anything pre-defined. When I got this originally from the documentation, the variable name was set to 'data' which I've been told is not really a good name for a variable and I believe them. I myself thought it was a function or something.

good luck

2 Likes

I am very new to retool.
Where would this script go? I tried making a button and adding the script there.

Followed this to set up and can delete one by one from firebase realtime db: https://www.youtube.com/watch?v=k8Tyt0nz4-g&list=PLvUajl9_RH8_14KMlo9NXsmD6Vu-Zm17h&index=3

Hey @teddy2sticks, (belated) welcome to Retool!

I imagine you may be farther along in your build by now but in case it's still helpful, it should be fine to put your script in a button using a script event handler. Otherwise, that script would typically go tin a JavaScript query which you can read more about here. That doc contains an example very similar to the code in this thread.

If you find it still isn't working for you, would you mind sharing screenshots and code snippets of your setup?