title: Remove objects in .GlobalEnv from within a function
body: <p>I would like to create a function (<code>CleanEnvir</code>) which basically calls remove/rm and which removes certain objects from <code>.GlobalEnv</code>.</p>
  
  <pre><code>  CleanEnvir &lt;- function(pattern = "tmp"){
        rm(list = ls()[grep("tmp", ls())], envir = globalenv())
    }
  
    keep &lt;- 1
    tmp.to.be.removed &lt;- 0
    ls()
  
    ## does not work
    CleanEnvir()
    ls()
  
    ## does work
    rm(list = ls()[grep("tmp", ls())], envir = globalenv())
    ls()
  </code></pre>