offlineimap delete folder

Offlineimap is tool for syncing mails. But if you delete remote folder after sync it appears again on remote account. I wrote small bash script what solves this problem.

Firstly it synchronizes account and parses all remote folders. It is up to you to remove paths you want preserve. Then it removes all folders on remote server and on local machine. At last it synchronizes and cleans up.

#!/bin/bash

account="Posvic"

download() {
  offlineimap -a $account &> output.txt
  cat output.txt | grep "Syncing" | cut -d' ' -f3,4 | cut -d':' -f1 > output2.txt
  mv output2.txt output.txt
}

delete() {
  while read i; do
    echo "Deleting $i..."
    offlineimap -a $account --delete-folder "$i"
    rm -rf "Mail/$account/${i//\//\.}"
  done < output.txt
}

if [ $# -ne 1 ]; then
  echo "Usage: $0 [download|delete|sync]"
  exit 1
fi

case "$1" in
  "download")
    download
    ;;

  "delete")
    delete
    ;;

  "sync")
    offlineimap -a $account
    rm output.txt
    ;;

  *)
    echo "Wrong parameter!"
    ;;
esac

Leave a Reply

Your email address will not be published.