title: Delete an entire data.table by reference
body: <p>Is it possible to delete a <code>data.table</code> object by reference? How could a function <code>rm_tbl()</code> be implemented that takes a <code>data.table</code> object and assigns <code>NULL</code> to any name pointing to this object in any environment (that is a descendant of the globalenv)?</p>
  
  <p>Examples, which for obvious reasons do not work, but perhaps convey the idea of what I am trying to achieve:</p>
  
  <pre><code>rm_tbl_1 &lt;- function(tbl) {
    rm(tbl)
    invisible(NULL)
  }
  
  rm_tbl_2 &lt;- function(tbl) {
    tbl &lt;&lt;- NULL
    invisible(NULL)
  }
  </code></pre>
  
  <p>The following comes close, but is a bit of a hack (also it does not result in <code>NULL</code> but a zero-row <code>data.table</code>)</p>
  
  <pre><code>rm_tbl_3 &lt;- function(tbl) {
    tbl[, colnames(tbl) := rep(list(NULL), ncol(tbl))]
    invisible(NULL)
  }
  </code></pre>
  
  <p>For completeness:</p>
  
  <pre><code>dt &lt;- data.table(a = 1:3, b = 2:4)
  
  rm_tbl_1(dt)
  dt
  rm_tbl_2(dt)
  dt
  rm_tbl_3(dt)
  dt
  </code></pre>
  
  <h2>Edit</h2>
  
  <p>As per upvoted suggestion by @Gregor, some further explanation: The problem I'm facing is that I have a large <code>data.table</code>. Now somewhere in a function, I do something to this object, e.g. call <code>data.table::split()</code> and I no longer have any need for the original <code>data.table</code>. Additionally, In order to do further transformations on my data, I need the memory back of the original <code>data.table</code>. How do I do that?</p>
  
  <p>An example:</p>
  
  <pre><code>fun_a &lt;- function() {
    dt &lt;- data.table(a = 1:2, b = 2:3)
    fun_b(dt)
  }
  
  fun_b &lt;- function(tbl) {
    temp &lt;- split(tbl, by = "b")
    rm_dt(tbl)
    do_stuff_with_dt(temp)
  }
  
  fun_a()
  </code></pre>
  
  <p>Does this clear things up? I'm sorry for not being clearer to begin with.</p>