title: Firebase Cloud Functions onDelete problem - Database
body: <p>I have the following structure in the Firebase database, where <code>RoomsTemplate</code> is a subscription room. If someone signs up for the <code>Users</code> node, another function (onWrite) increases the value of <code>CountUsers</code>.</p>
  <p><a href="https://i.stack.imgur.com/nJy04.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nJy04.png" alt="enter image description here" /></a></p>
  <p>and the following function decreases the value of <code>CountUsers</code> when users unsubscribe from node <code>Users</code>.</p>
  <pre><code>exports.recountUsers = functions.database.ref('/RoomsTemplate/{id}/CountUsers').onDelete(async (snap) =&gt; {
    const counterRef = snap.ref;
    const collectionRef = counterRef.parent.child('Users');
    const messagesData = await collectionRef.once('value');
    return await counterRef.set(messagesData.numChildren());
  });
  </code></pre>
  <p>The problem is that, if I want to delete the main node of the <code>RoomsTemplate</code>, in this case</p>
  <blockquote>
  <p>-MBb2ZKj-pquagHBqguS</p>
  </blockquote>
  <p>the <code>onDelete</code> function won't let me delete it, but it will update the <code>CountUsers: 0</code> again. Try deleting the <code>Barrio:&quot;Barcelona&quot;</code> node with another function first and then verify and delete the main node.</p>
  <pre><code>exports.recountUsers = functions.database.ref('/RoomsTemplate/{id}/CountUsers').onDelete(async (snap) =&gt; {
    const counterRef = snap.ref;
    const collectionRef = counterRef.parent.child('Users');  
    const messagesData = await collectionRef.once('value');
  
    const Barrio = await counterRef.parent.child('Barrio').once('value');
  
     if(Barrio.numChildren() === 0){
       return await counterRef.remove();
     }else if(Barrio.numChildren() &gt; 0){
       return await counterRef.set(messagesData.numChildren());
     }else{
       return null;
     } 
  
  });
  </code></pre>
  <p>But I still have the same existence problem. <strong>How can I permanently delete the main node (-MBb2ZKj-pquagHBqguS), without executing the onDelete function?</strong></p>