Delete buffer at the right time