title: Firebase admin deleteUser function not working
body: <p>I'm trying to delete all the users in my auth and database using firebase functions. Here's my code for that:</p>
  <pre><code>const admin = require(&quot;firebase-admin&quot;);
     
  admin.initializeApp({
    credential: admin.credential.applicationDefault(),
    databaseURL: &quot;----&quot;
  });
  export const listenToAdminCommands = functions.firestore.document('collection/{docUid}')
    .onWrite((change, context) =&gt;
    {
      const pass: string = '--';
      // const before = change.before.exists? change.before.data() : null;
      const after = change.after.exists? change.after.data() : null;
  
      if(after !== null &amp;&amp; after !== undefined) {
        const adminCommandType: string = after['type'];
        const adminCommandPass: string = after['pass'];
  
        if(adminCommandType === 'deleteAll' &amp;&amp; adminCommandPass === pass) {
          adminDeleteAllUsers();
        }
      }
    });
  //Admin control
  function adminDeleteAllUsers() {
    deleteAllUsers(' ');
    return null;
  }
  function deleteAllUsers(nextPageToken: any) {
    admin.auth().listUsers(1000, nextPageToken)
      .then((listUsersResult: any) =&gt; {
        //go through each one and check last time signed in
        listUsersResult.users.forEach((userRecord: any) =&gt; {
          const user: any = userRecord.toJSON();
          const userUid = user['uid'];
          console.log('Deleting user for data delete uid = ', userUid);
          admin.auth().deleteUser(userUid)
            .then(() =&gt; {
              console.log('Successfully deleted user', userUid);
            })
            .catch((error: any) =&gt; {
              console.log('Error deleting user:', error);
            });
            db.collection('users').doc(userUid).delete();
        });
        if (listUsersResult.pageToken) {
          // List next batch of users.
          listAllUsers(listUsersResult.pageToken);
        }
      })
      .catch((error: any) =&gt; {
        console.log('Error listing users:', error);
      });
  }
  </code></pre>
  <p>When the function get executed, no user is deleted. It's like the function never worked. Am I missing something?</p>
  <p>Update:
  I'm not sure if this is the way to do it, but it's still not working. I tried to handle promises correctly, but I'm not sure if what I'm doing is correct or not.</p>
  <pre><code>export const listenToAdminCommands = functions.firestore.document('collection/{docUid}')
    .onWrite((change, context) =&gt;
    {
      const pass: string = '---';
      // const before = change.before.exists? change.before.data() : null;
      const after = change.after.exists? change.after.data() : null;
  
      if(after !== null &amp;&amp; after !== undefined) {
        const adminCommandType: string = after['type'];
        const adminCommandPass: string = after['pass'];
  
        if(adminCommandType === 'deleteAll' &amp;&amp; adminCommandPass === pass) {
          return adminDeleteAllUsers();
        }
        return;
      }
      return;
    });
  //Admin control
  function adminDeleteAllUsers() {
    return deleteAllUsers(' ');
  }
  function deleteAllUsers(nextPageToken: any) {
    return admin.auth().listUsers(1000, nextPageToken)
      .then((listUsersResult: any) =&gt; {
        //go through each one and check last time signed in
        listUsersResult.users.forEach((userRecord: any) =&gt; {
          const user: any = userRecord.toJSON();
          const userUid = user['uid'];
          console.log('Deleting user for data delete uid = ', userUid);
          return admin.auth().deleteUser(userUid)
            .then(() =&gt; {
              console.log('Successfully deleted user', userUid);
              return db.collection('users').doc(userUid).delete();
            })
            .catch((error: any) =&gt; {
              console.log('Error deleting user:', error);
              return;
            });
        });
        if (listUsersResult.pageToken) {
          // List next batch of users.
          listAllUsers(listUsersResult.pageToken);
        }
        return;
      })
      .catch((error: any) =&gt; {
        console.log('Error listing users:', error);
        return;
      });
  }
  </code></pre>