Tech Blog

SCRIPT: Automatic CouchDB Compaction

As you probably know CouchDB will not automatically reclaim unused space, nor compact or clean up its views during normal operation.  Instead these...

Published
January 2, 2012
Author
Adam Strohl
Reading time
1 minute

SCRIPT: Automatic CouchDB Compaction

As you probably know CouchDB will not automatically reclaim unused space, nor compact or clean up its views during normal operation.  Instead these processes are meant to be run at optimal times (ie; off peak hours) so they do not affect user performance during critical periods.

Over time this obviously means that any CouchDB can grow to be huge unless it is tended to.

This script is designed to be run from a scheduler (like cron) and target a CouchDB instance/URL.  It first fetches all the DBs inside it (except the special _underscore ones) and then trigger a DB compaction.  For each DB all the views inside are given the cleanup (for old versions of views) and a compaction commands as well.  To my knowledge this is all of the compaction/cleanup processes CouchDB does.

Note: The script will exit fairly quickly because CouchDB will simply acknowledge the commands and then go off and process the requests (which can take some time).  Just because the script finished does not indicate that the compaction has completed.

Feel free to post questions or report problems in the comments!

We’ve created a Github project for this: ateamsystems/ateam_couchdb_compact

Grab it via GitHub:

git clone https://github.com/ateamsystems/ateam_couchdb_compact.git

Need help with Linux or FreeBSD infrastructure?

A-Team Systems provides engineer-led support for production Linux and FreeBSD environments, including troubleshooting, operational oversight, and ongoing infrastructure management.

Contact A-Team Systems