<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Pratik Shah</title>
    <description>The latest articles on DEV Community by Pratik Shah (@pratik14).</description>
    <link>https://dev.to/pratik14</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F440830%2F2df0c20e-feaa-4fb3-aeb2-34e1665201e7.jpeg</url>
      <title>DEV Community: Pratik Shah</title>
      <link>https://dev.to/pratik14</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pratik14"/>
    <language>en</language>
    <item>
      <title>Deleting all Firebase users</title>
      <dc:creator>Pratik Shah</dc:creator>
      <pubDate>Sat, 17 Apr 2021 08:08:40 +0000</pubDate>
      <link>https://dev.to/pratik14/deleting-all-firebase-users-l4d</link>
      <guid>https://dev.to/pratik14/deleting-all-firebase-users-l4d</guid>
      <description>&lt;p&gt;There are 100's of zombie users in my Firebase account and I was looking for efficient way of deleting all Firebase users. &lt;/p&gt;

&lt;p&gt;There are three ways in which we can delete users in Firebase&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Using Client SDK: in which we can only delete the current user account
&lt;code&gt;firebase.auth().currentUser?.delete();&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Login to Firebase console and delete user one by one from the web page. There is no bulk delete option.&lt;/li&gt;
&lt;li&gt;Delete all user at once using Admin SDK &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I used third option because it is fastest. Let's go through steps of writing script to delete users using Admin SDK.&lt;/p&gt;

&lt;p&gt;We need to write simple &lt;code&gt;node.js&lt;/code&gt; script in which we will initialise Admin SDK and interact with Firebase in privileged environment.&lt;/p&gt;

&lt;p&gt;Before this we will need to generate private key which is used in initialising Admin SDK. Below steps are copied form Firebase docs to generate key&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;To generate a private key file for your service account:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In the Firebase console, open Settings &amp;gt; Service Accounts.&lt;/li&gt;
&lt;li&gt;Click Generate New Private Key, then confirm by clicking Generate Key.&lt;/li&gt;
&lt;li&gt;Securely store the JSON file containing the key.&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now we have the private key, lets start writing node script&lt;/p&gt;

&lt;p&gt;Create a new folder. Run the following in terminal&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm init
sudo npm install firebase-admin --save
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now create an &lt;code&gt;index.js&lt;/code&gt; file in this folder and copy the below script. Replace &lt;code&gt;serviceAccount&lt;/code&gt; file path and &lt;code&gt;databaseURL&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const admin = require("firebase-admin");
const serviceAccount = require("./path/to/serviceAccountKey.json");
const databaseURL = "https://project-name.firebaseio.com"

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: databaseURL
});

const deleteAllUsers = (nextPageToken) =&amp;gt; {
  let uids = []
  admin
    .auth()
    .listUsers(100, nextPageToken)
    .then((listUsersResult) =&amp;gt; {
      uids = uids.concat(listUsersResult.users.map((userRecord) =&amp;gt; userRecord.uid))
      console.log(uids)
      if (listUsersResult.pageToken) {
        deleteAllUsers(listUsersResult.pageToken);
      }
    })
    .catch((error) =&amp;gt; {
      console.log('Error listing users:', error);
    }).finally(() =&amp;gt; {
      admin.auth().deleteUsers(uids)
    })
};

deleteAllUsers();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above scripts fetch users in batch and collects uid of users. Which then are passed to &lt;code&gt;deleteUsers&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;Above script will delete all the users in Firebase. &lt;/p&gt;

</description>
      <category>firebase</category>
    </item>
  </channel>
</rss>
