Question

I would like to delete or remove the contents of a tempfile within a .do file so as to conserve on memory. I know that the file is supposed to be cleared upon the .do file's completion, but I am wondering whether I can remove the contents mid-way after no longer needing it.

The thing that comes to mind is to just open the file. Delete all of the observations (I know an upper bound on the size of the file a priori). And then do a save-replace so as to decrease the size of the file. While that works, I doubt it is the most efficient solution since it requires opening a potentially large file, roughly knowing the size of the file, and the file continues to exist albeit by only a small amount.

This is the code I generated to delete the tempfile:

  tempfile temp1 temp2
  /* LOTS OF CODE HERE */
  clear
  set mem 1197m
  foreach X in temp1 temp2 {
    clear
    set mem 1197m
    use "``X''"
    drop if _n <= 20000000
    save "``X''", replace
  }
Was it helpful?

Solution

If you really need to delete a temporary file, just use erase. erase is Stata's command for deleting files across platforms. Naturally Stata will talk to whatever operating system it is to do that.

Your code assumes that a temporary file will be a Stata .dta file, never an assumption that Stata makes. But that's a detail: you don't need anything so bizarre or round-about.

OTHER TIPS

There is no point in deleting the temporary files. They are on your hard drive, not in your memory, so if anything, sending the command to the operating system to do anything with that file (use, erase, ls, whatever) is an unnecessary toll on your computing time, especially if you have 20M observations that will take a few seconds to load even on the fastest server. The only reason I could see in deleting the temp files is that if you are on a network with very strict usage quotas, and every temp file takes up half of your remaining quota -- in other words, when saving `temp2' could throw you over the quota and produce an error message that you could have otherwise avoided. If you are NOT in that situation, you can just leave these files be. Stata will take better care of cleaning these files after you than you could yourself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top