Image for post
Image for post

Firebase: Fetching and deleting user accounts in bulk

Hiranya Jayathilaka
May 8, 2020 · 3 min read

Firebase Admin SDK contains APIs for fetching and deleting individual user accounts. These APIs enable developers to fetch Firebase Auth user accounts by their uid, email or phone number, or delete a selected user account by its uid. Developers have been using these APIs to implement tools, automation scripts and even Cloud Functions that perform complex user management tasks. Listing 1 shows a Node.js script that uses the Admin SDK to fetch a user account by email address, and delete it.

Listing 1: Fetching and deleting a single user account

While these APIs are useful in a wide range of use cases, sometimes we need to implement even more powerful backend applications that deal with multiple user accounts at once. As a developer you may wish to fetch several user accounts as a batch, and perform some administrative operations on them. Or you may have been running some experiments with Firebase that created a bunch of test accounts, and now you wish to delete all of them.

Implementing such bulk operations using the existing getUser() and deleteUser() APIs is rather cumbersome. The end result is also quite slow to execute since each API call results in a new RPC call being made. You can make things a little better by using the listUsers() API in your code, but that’s not a great solution either as it doesn’t allow you to filter the user accounts that are returned. As a result many developers have been asking for a simpler API to fetch and delete Firebase Auth user accounts in bulk. If you are one of those developers, I have some really good news for you!

Starting from v8.12.0 release of the Firebase Admin Node.js SDK, developers have two new APIs — getUsers() and deleteUsers() — available to them. As evident by their names, these APIs can be used to fetch and delete more than one user account at a time. The getUsers() API accepts up to a hundred user identifiers, and returns the matching user accounts. The user identifier could be a Firebase uid, email address, phone number or an IdP-assigned uid. Similarly the deleteUsers() API accepts up to a thousand Firebase uids, and deletes those user accounts from the project. Both APIs only make a single RPC to the backend, and therefore are more efficient than calling the old getUser() or deleteUser() APIs in a loop. Listing 2 shows an example of how these APIs can be used in practice.

Listing 2: Fetching and deleting a batch of user accounts

This example program looks up several user accounts, and goes over the results to mark the already disabled accounts for deletion. Then it uses the deleteUsers() API to delete all the marked user accounts. This example makes at most 2 RPC calls to the Firebase Auth backend despite the number of user accounts fetched or deleted.

One caveat to note is that the deleteUsers() API is rate limited to 1 call per second. This might change in the future, but at least for now this means you can only delete at most 1000 accounts per second using this API.

I hope this addition simplifies the development of many advanced user management features. Please try it out and provide feedback via GitHub. More code samples and language support (Java, Python, Go and C#) are expected soon.

Firebase Developers

Sign up for The Firebase Developers Quarterly

By Firebase Developers

A summary of what has happened on the Firebase Developers publication in the past quarter. Sent once a quarter. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Hiranya Jayathilaka

Written by

Software engineer at Google. Enjoys working at the intersection of cloud, mobile and programming languages. Fan of all things tech and open source.

Firebase Developers

Tutorials, deep-dives, and random musings from Firebase developers all around the world. Views expressed are those of the authors and don’t necessarily reflect those of Firebase or its parent companies.

Hiranya Jayathilaka

Written by

Software engineer at Google. Enjoys working at the intersection of cloud, mobile and programming languages. Fan of all things tech and open source.

Firebase Developers

Tutorials, deep-dives, and random musings from Firebase developers all around the world. Views expressed are those of the authors and don’t necessarily reflect those of Firebase or its parent companies.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store