Delete object from TFile

I have a .root file without directories in which I store many objects. I would like to delete some of them, so I do:

TFile *file = new TFile("file.root","update");

for(int i=150;i<200;i++)

After that I no longer see these objects in file.root, but the size of this file on disk is still the same as before deleting objects. What can I do to reduce file’s size after deleting objects from it?

ROOT cannot shrink an existing file (the “unused” / “freed” space is marked as such and it wil be reused when writing new objects to this file). If you want a shorter file, you have to create a “new” file and copy the required objects from the “old” file.