0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Gmailで7日経った下書きメールはGASで自動で削除する

Posted at

はじめに

こんにちは。実家の畑を勝手に耕しているおじさん問題で、毎朝1時間おきにパトロールしに行ってます、筆者です :innocent:

さて、Gmailに気づいたら下書きメールが溜まっていたなんてことはないでしょうか?
自動で保存してくれるので、スマホの電池が途中で切れたり間違ってアプリ起動落としちゃったりしたときに、下書きとして残っているのはありがたいことです。

ただ、それで気づいたら下書きメールが溜まっていたりします...。
いつのなんのメール何だこれ?:thinking:

なので、7日経った下書きはもう消してしまうことにします:pray:

Gmailで7日経った下書きメールはGASで自動で削除する

コードはシンプルでこれで行けます:ok_hand:

const main = () => {
  const now = new Date()
  const messages = GmailApp.getDraftMessages()
  for (let i in messages) {
    if (messages[i].getDate() < new Date(now.getFullYear(), now.getMonth(), now.getDate() - 7)) {
      messages[i].moveToTrash()
    }
  }
}

トリガーを毎日セット

a.png

補足

以下の記事のコードとがっちゃんこすると、トリガー1つでいいかもです!

おわりに

気づいたらゴミ溜まっているなんてことが多いGmail。自動でお掃除するのって大事だと思います:v:
それでは!

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?