Move database cleaning to lib so it can be shared