Skip to content

Instantly share code, notes, and snippets.

@pguelpa
Created September 16, 2015 22:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pguelpa/2f47a8679a560eac6009 to your computer and use it in GitHub Desktop.
Save pguelpa/2f47a8679a560eac6009 to your computer and use it in GitHub Desktop.
Postgres limited delete with filter
export BATCH_SIZE=100000
export PGPASSWORD=xyz
export PGHOST=x.y.z
export PGUSER=abc
export PGDATABASE=def
count=0
until psql -c "DELETE FROM <table> WHERE id IN (SELECT id FROM <table> WHERE <time filter> <= NOW() - INTERVAL '24 hours' LIMIT ${BATCH_SIZE});" | grep -v "DELETE ${BATCH_SIZE}"
do
count=$((count + BATCH_SIZE))
echo "Deleted: ${count}"
sleep 5
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment