Skip to content

Instantly share code, notes, and snippets.

@systra
Created December 8, 2013 21:35
Show Gist options
  • Save systra/7864133 to your computer and use it in GitHub Desktop.
Save systra/7864133 to your computer and use it in GitHub Desktop.
CouchDB cleanup - remove old documents (shell method)
#!/bin/bash
# source: http://bravenewmethod.com/2012/05/31/couchdb-cleanup-script-for-purging-old-docs/
# but modified to get rid of node.js dependency
# if database needs auth:
# DBHOST=user:pass@hostname
DBHOST=localhost
PORT=5984
# Get key for entries that are over 1 month old.
# This assumes that created view can be queried using timestamps as keys.
if uname -a | grep -i darwin > /dev/null
then
TODAY=$(date '+%Y-%m-%d')
MONTHSAGO=$(date -v-1m '+%Y-%m-%d')
MONTHSAGO_E=$(date -v-1m '+%s')
else
TODAY=$(date '+%Y-%m-%d')
MONTHSAGO=$(date -d '1 month ago' '+%Y-%m-%d')
MONTHSAGO_E=$(date -d '1 month ago' '+%s')
fi
cleanup() {
DATABASE=$1
DESIGN=$2
VIEW=$3
echo "Cleaning $DATABASE/$DESIGN"
curl --silent -S http://$DBHOST:$PORT/$DATABASE/_design/$DESIGN/_view/$VIEW?endkey=$MONTHSAGO_E | \
awk 'BEGIN { FS=OFS=","; print "{\"docs\":["; } /"id"/ { gsub(/id/,"_id"); gsub(/value/,"_rev"); if(id) print id ", \"_deleted\":true, " rev ","; id = $1; rev = $3; } END { if (id) print id ", \"_deleted\":true, " rev; print "]}"; }' | \
curl --silent -S -X POST -d @- -H "Content-Type:application/json" http://$DBHOST:$PORT/$DATABASE/_bulk_docs | \
sed 's/\({[^}]*}\),/\1=/g' | \
tr "=" "\n" | tr -d '[]' | \
wc -l | awk '{ print $1 " docs deleted" }'
#grep -v "\"ok\":true"
}
echo "STATS CLEANUP <= $MONTHSAGO - Start" `date`
# Put databases and views here
cleanup mydb history outdated
echo "STATS CLEANUP - Done" `date`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment