title: firebase RTDB transactional delete
body: <p>I have been using firestore transactions quite well and have been trying to implement some RTDB versions.</p>
  <p>I have a tree with autogenerated keys. The values of these keys are maps, whereby one of the keys is 'uid' e.g.</p>
  <pre><code>&quot;AUTOGENKEY&quot; : {
      &quot;uid&quot; : 'a uid'
  }, 
  ...etc
  </code></pre>
  <p>I want to have a transaction where i can delete all of a singles users nodes... if any new nodes are created by the user during the transaction it should retry and include the new nodes in the transaction delete.</p>
  <p>I currently have this</p>
  <pre><code>
  await rtdb.ref(‘someRef’)
      .orderByChild(‘uid’)
      .equalTo(uid)
      .once('value')
      .transaction(function(currentVal) {
              // Loop each of the nodes with a matching ‘uid’ and delete them
              // If any of the nodes are updated (or additional nodes are created with matching uids)
             // while the transaction is running it should restart and retry the delete        
             // If no nodes are matched nothing should happen    
      });
  </code></pre>
  <p>But I wanted to double check whether i need another transaction inside the currentVal callback and whether or not i can just return null to delete each of the nodes.</p>
  <p>I have been using this answer as a reference <a href="https://stackoverflow.com/questions/53568052/firebase-database-transactional-search-and-update">Firebase database transactional search and update</a></p>
  <p>Kind regards</p>
  <p>-Edit for new method</p>
  <p>I have taken your advide frank and have ended up just storing my data like so:</p>
  <pre><code>uid -&gt; counter 
  </code></pre>
  <p>I didn't know transactions didnt cannot run on queries, thanks for letting me know.</p>
  <p>I need to be able to add/subtract quantities from the uids count and if it ever results in a number below 0 the node should be deleted. If i pass null as an amount it should delete the node. This is what i currently have.</p>
  <pre><code>async function incrementOrDecrementByAmount(pathToUid, shouldAdd, amount, rtdb){
  
    await rtdb.ref(pathToUid)
      .transaction(function(currentVal) {
     
        if(currentVal == null || amount == null) {
          return amount;
        }else{
          let newAmount = null;
  
          // Just sum the new amount
          if(shouldAdd == true) {
            newAmount = currentVal + amount;
          } else {
            const diff = currentVal - amount;
  
            // If its not above 0 then leave it null so it is deleted
            if(newAmount &gt; 0) {
              newAmount = diff;
            }
          }
  
          return newAmount;
        }
      });
  }
  
  
  </code></pre>
  <p>I am not sure about the first if statement, if I have the following execution.</p>
  <ol>
  <li>incrementOrDecrementByAmount(somePath, 10, true, rtdb)</li>
  <li>incrementOrDecrementByAmount(somePath, 100, false, rtdb)</li>
  </ol>
  <p>Will this always result in the node being deleted? are the transaction always dependent on the call order or is it a race condition on who finishes first.</p>