title: Write a function to remove everything in the global environment
body: <p>I want to write a function to remove everything from the global environment. But <code>rm(list = ls(all = TRUE))</code> is not working within a function. </p>
  
  <pre><code>z = 1:10
  
  clean.mem &lt;- function() {
    rm(list = ls(all = TRUE))
    }
  clean.mem()
  </code></pre>
  
  <p>I also try:</p>
  
  <pre><code>clean.mem &lt;- function() {
    rm(list = ls(all = TRUE), envir = globalenv(), pos = globalenv())
    }
  </code></pre>
  
  <p>but it still fails. What should I do?</p>