private void ScavengeByNumber(int deleteBatchSize) { if (ConfigurationManager.AppSettings["MaxDocsInDatabase"] == null) return; var maxDocs = long.Parse(ConfigurationManager.AppSettings["MaxDocsInDatabase"]); var options = new ViewOptions(); options.StartKey.Add(MessageIdManager.Create(new DateTime(1970, 1, 1)).ToDocId()); options.EndKey.Add(MessageIdManager.Create(DateTime.UtcNow).ToDocId()); var db = new CouchClient(ConfigurationManager.AppSettings["HydraServer"], int.Parse(ConfigurationManager.AppSettings["Port"]), null, null, false, AuthenticationType.Basic). GetDatabase(ConfigurationManager.AppSettings["Database"]); // Getting the empty document returns general database info var deleteCount = db.GetDocument("").Value<long>("doc_count") - maxDocs; while (deleteCount > 0) { options.Limit = (int) Math.Min(deleteCount, deleteBatchSize); var rows = db.GetAllDocuments(options).Rows; if (!rows.Any()) break; DeleteDocs(rows, db); deleteCount -= deleteBatchSize; } }