Survival of molecular gas in cavities of transition disks - I. CO