--- /dev/null
+#!/bin/bash
+
+#This script is to delete any purchase orders that are pending, named delete, and have no line items or direct charges.
+
+#set up the environment
+PSQL="/usr/bin/psql"
+DB_USER="mydbuser"
+DB_HOST="mydbhost"
+read -r -d '' SQL << 'EOF'
+DELETE FROM acq.purchase_order
+WHERE id IN (SELECT po.id FROM acq.purchase_order po
+ LEFT JOIN acq.lineitem li ON (li.purchase_order = po.id)
+ LEFT JOIN acq.po_note note ON (note.purchase_order = po.id)
+ LEFT JOIN acq.po_item poi on (poi.purchase_order = po.id)
+WHERE li.id IS NULL
+ AND poi.id IS NULL
+ AND note.id IS NULL
+ AND po.state = 'pending'
+ AND po.name ILIKE '%delete%')
+EOF
+
+#do the thing
+
+$PSQL -U $DB_USER -h $DB_HOST -1 -c "$SQL"