Factor out directory cleanup to common function library