properly close metadata document