Here's a variety of attempts at answering the same question, with varying levels of sophistication thrown into the mix.
Way Too Simple
This is the equivalent of shutting your computer down every day by pulling the plug out. It does what it promises but there's considerable damage as a side-effect.
Bit Too Simple
The second attempt alleviates some of the collateral damage but has flaws of its own. If the close fails, then we have no way to fall into the error case.
Next time: Detecting Metadata