Deleting trees/ntuples from a file doesn't free up space

Hi there,

I’m writing several TNtuples to a file, performing operations on them, and then deleting them from the file. When I delete them however, the space doesn’t seem to get freed up even though the TTrees are no longer visible in the TBrowser.

I’ve tried using TFile->Delete(“foo;*”); and TNtuple->Delete(“all”); as listed in the class reference without any luck. Can anyone point me in the right direction?

Below is a simple root macro that replicates the problem. The filesize of the ntuple doesn’t seem to change from 3.9MB whether lines 20 and 21 are commented out or not. If I try using only line 20, the ntuple is still visible in the TBrowser. If I use line 21 only the ntuple disappears but takes up the same size.

#include <TFile.h>
#include <TNtuple.h>
#include <TROOT.h>
#include <TSystem.h>
#include <TRandom.h>
#include <TInterpreter.h>

int testDelete(){
TFile *f = new TFile("test.root","RECREATE","test");
TNtuple *ntuple = new TNtuple("ntuple","Demo ntuple","px:py:pz:random:i");
Float_t px, py, pz;
for (Int_t i = 0; i < 25000; i++){
        pz = px*px + py*py;
        Float_t random = gRandom->Rndm(1);

ntuple->Delete("all"); //As listed in TTree docs, but this doesn't work
f->Delete("ntuple;*"); //As listed in TFile docs, but this doesn't work


the gaps created when deleting objects can only be reused by you close and reopen the file.


I’m not sure I understand. Do you mean If I write this file out after deleting the ntuple, reopen it using “UPDATE” and write it once more, the filesize will shrink?

Could you provide a brief example, perhaps based on the macro I supplied that results in a reduction in size of the created TFile once the ntuple is deleted?



The file size will never shrink. The deleted gaps will be reused.


I see, so if for example I have a file containing a few k of histograms and a 1GB ntuple, I cannot shrink the file to contain only the histograms…

In that case I should write the ntuple to a temporary file and physically delete it from disk when finished I guess.



The simplest solution is to create another small file containing only the histograms.
-delete the ntuple;1 in filebig.root
-close the file
-run, eg, the hadd utility like
hadd -f filesmall.root filebig.root


1 Like

Thanks, but I’m trying to do this all in a single binary.

In the end, I’m using stdio’s tmpnam function to name a temporary file, and writing the ntuples to it at the same time as writing the histograms to a different file. At the end of the code I remove the temporary file.