# Cleaning up e-mail logs

**URL:** https://meta.discourse.org/t/cleaning-up-e-mail-logs/39132
**Category:** Support
**Created:** [February 6, 2016, 1:51am UTC](https://meta.discourse.org/t/cleaning-up-e-mail-logs/39132 "2016-02-06T01:51:51Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![garthomite](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/garthomite/32/121989_2.png) [@garthomite](https://meta.discourse.org/u/garthomite)
#### Post date: [February 6, 2016, 1:51am UTC](https://meta.discourse.org/t/cleaning-up-e-mail-logs/39132/1 "2016-02-06T01:51:51Z")

</div>

I was looking to free up some disk space on our discourse installation and I noticed that the email\_logs table was using a substantial amount of space compared to everything else. For context all other tables amount to about 100mb but the e-mail logs are in the range of 12GB

I’m curious, is there a process to limit the amount in this table? Can older entries be removed on a regular basis?

Thanks!

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [February 6, 2016, 2:27am UTC](https://meta.discourse.org/t/cleaning-up-e-mail-logs/39132/2 "2016-02-06T02:27:04Z")

</div>

Sounds like there was a dire problem with your email config at one point?

---

<div class="post-metadata">

### Author: ![garthomite](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/garthomite/32/121989_2.png) [@garthomite](https://meta.discourse.org/u/garthomite)
#### Post date: [February 6, 2016, 2:56am UTC](https://meta.discourse.org/t/cleaning-up-e-mail-logs/39132/3 "2016-02-06T02:56:54Z")

</div>

Diving in to this a little more there are a lot of logs with the type ‘email\_reject\_destination’ with a lot of spam content, ~ 500k rows in one week.

Is there any harm in selectively trimming the table either by just date or perhaps just removing any ‘email\_reject\_destination’ logs over x days?

**EDIT** - Yep, a lot of reject logs!

```
  count | email_type              
----------+--------------------------------------
 24086049 | email_reject_destination
  6293124 | email_reject_auto_generated
   639048 | mailing_list
    55853 | digest
     7398 | user_posted

```

`

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [February 6, 2016, 10:18am UTC](https://meta.discourse.org/t/cleaning-up-e-mail-logs/39132/4 "2016-02-06T10:18:59Z")

</div>

> [@garthomite](#):
>
> Is there any harm in selectively trimming the table either by just date or perhaps just removing any ‘email\_reject\_destination’ logs over x days?

You can safely trim all the `email_logs` which have an **empty** `reply_key` (eg. “`email_reject_*`” and “`digest`”).

For the ones that have a `reply_key`, if you delete them, you’ll prevent users from replying to that email. So I guess you could be fine deleting some that are older than X months.

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [February 6, 2016, 10:25am UTC](https://meta.discourse.org/t/cleaning-up-e-mail-logs/39132/5 "2016-02-06T10:25:08Z")

</div>

Should we be doing this automatically?

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [February 6, 2016, 10:27am UTC](https://meta.discourse.org/t/cleaning-up-e-mail-logs/39132/6 "2016-02-06T10:27:16Z")

</div>

We could, but I’m not sure what threshold to use. Add a new site setting which defaults to 1 year?

Note: this will also affect the stats we have on the dashboard.

---

<div class="post-metadata">

### Author: ![garthomite](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/garthomite/32/121989_2.png) [@garthomite](https://meta.discourse.org/u/garthomite)
#### Post date: [February 6, 2016, 3:56pm UTC](https://meta.discourse.org/t/cleaning-up-e-mail-logs/39132/7 "2016-02-06T15:56:15Z")

</div>

After trimming the rejects our database shrunk from 13gb → 900mb so our database is happy again.

Thanks!

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [February 6, 2016, 8:44pm UTC](https://meta.discourse.org/t/cleaning-up-e-mail-logs/39132/8 "2016-02-06T20:44:30Z")

</div>

Sure default to 1 year is safe lets do that!

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [February 8, 2016, 5:48pm UTC](https://meta.discourse.org/t/cleaning-up-e-mail-logs/39132/9 "2016-02-08T17:48:27Z")

</div>

[https://github.com/discourse/discourse/commit/ac863bab915a1b808e259d3bfd0d23b7bbba13ca](https://github.com/discourse/discourse/commit/ac863bab915a1b808e259d3bfd0d23b7bbba13ca)

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [February 8, 2016, 8:33pm UTC](https://meta.discourse.org/t/cleaning-up-e-mail-logs/39132/10 "2016-02-08T20:33:41Z")

</div>

Suppress? Isn’t the “purge” or “delete”?

I think the word is a bit confusing in this context

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [February 8, 2016, 8:36pm UTC](https://meta.discourse.org/t/cleaning-up-e-mail-logs/39132/11 "2016-02-08T20:36:22Z")

</div>

I was being consistent with [discourse/config/site\_settings.yml at main · discourse/discourse · GitHub](https://github.com/discourse/discourse/blob/master/config/site_settings.yml#L503) but I agree, `delete` is much better. (“`purge`” might be too technical)

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [February 8, 2016, 8:53pm UTC](https://meta.discourse.org/t/cleaning-up-e-mail-logs/39132/12 "2016-02-08T20:53:42Z")

</div>

Yeah it should be delete.

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [February 8, 2016, 9:19pm UTC](https://meta.discourse.org/t/cleaning-up-e-mail-logs/39132/13 "2016-02-08T21:19:22Z")

</div>

[https://github.com/discourse/discourse/commit/460665895c91b2f9018e361b393d7e00dc86b418](https://github.com/discourse/discourse/commit/460665895c91b2f9018e361b393d7e00dc86b418)

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [February 23, 2016, 6:19pm UTC](https://meta.discourse.org/t/cleaning-up-e-mail-logs/39132/14 "2016-02-23T18:19:54Z")

</div>

Hey @zogstrip,

When you changed from `suppress_email_logs_after_days` to `delete_email_logs_after_days` are the changed values lost on upgrades?

I just sent 15k e-mail to people :sadpanda: when digest ran.

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [February 23, 2016, 6:25pm UTC](https://meta.discourse.org/t/cleaning-up-e-mail-logs/39132/15 "2016-02-23T18:25:48Z")

</div>

Having a second look at it, I think you’re right. I forgot a migration… 😞

What did you set it to?

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [February 23, 2016, 6:29pm UTC](https://meta.discourse.org/t/cleaning-up-e-mail-logs/39132/16 "2016-02-23T18:29:02Z")

</div>

I have 110k users created trough API, and only 15k using the forum.

Just enabled digests last week, and set it to 60 days, because my non-users where created 5 months ago.

When the update ran, the default was set back to 360 days, and many e-mails sent.

I got back from my lunch and got to kill the sidekiq queue with 15k sent 🙂.

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [February 23, 2016, 6:31pm UTC](https://meta.discourse.org/t/cleaning-up-e-mail-logs/39132/17 "2016-02-23T18:31:05Z")

</div>

So sorry about that 😧

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [February 23, 2016, 8:38pm UTC](https://meta.discourse.org/t/cleaning-up-e-mail-logs/39132/18 "2016-02-23T20:38:02Z")

</div>

Can we fix it so people who upgrade today no longer have this problem

Ahhh re read … Odds on someone bring on that exact version with the bad name is really low

---

<div class="post-metadata">

### Author: ![tgxworld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tgxworld/32/106117_2.png) [@tgxworld](https://meta.discourse.org/u/tgxworld)
#### Post date: [May 10, 2018, 7:55am UTC](https://meta.discourse.org/t/cleaning-up-e-mail-logs/39132/19 "2018-05-10T07:55:01Z")

</div>

The default for `SiteSetting.delete_email_logs_after_days` has been reduced to 90 days as per

[https://github.com/discourse/discourse/commit/186623acd070b71422dfe61e083fa3aeb67844ac](https://github.com/discourse/discourse/commit/186623acd070b71422dfe61e083fa3aeb67844ac)

For busy sites that send out alot of emails, this table can bloat fairly quickly. There are some optimizations that we can do on the table to make it use less disk space but that’ll have to wait till [Discourse Version 2.1](https://meta.discourse.org/t/discourse-version-2-1/86339).

---

<div class="post-metadata">

### Author: ![tgxworld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tgxworld/32/106117_2.png) [@tgxworld](https://meta.discourse.org/u/tgxworld)
#### Post date: [July 24, 2018, 7:53am UTC](https://meta.discourse.org/t/cleaning-up-e-mail-logs/39132/21 "2018-07-24T07:53:35Z")

</div>

A couple of changes have been made here

1. [Drop `EmailLogs#topic_id`. · discourse/discourse@01a63f8 · GitHub](https://github.com/discourse/discourse/commit/01a63f8b4b3d193ecf9d25a8f7a0365c2902f0aa)  
This column was not required as we can obtain the topic through the post.

2. [PERF: Split skipped email logs into a seperate table. · discourse/discourse@ae8b0a5 · GitHub](https://github.com/discourse/discourse/commit/ae8b0a517ffda0bb57a67bb9773bfb441181dcee)  
Emails that have been skipped are logged into another table `skipped_email_logs` from now on so that we can avoid creating indexes like [this](https://github.com/discourse/discourse/blob/01a63f8b4b3d193ecf9d25a8f7a0365c2902f0aa/app/models/email_log.rb#L102). Also, we were previously storing the skipped reasons as strings in the database which was inefficient and the “locale” of the reason is decided when the record is created. Going forward, each skipped email log contains a `reason_type` which is stored as an integer in the database. The reason is then translated on demand based on the given `reason_type`.

3. [PERF: Move `EmailLog#reply_key` into new `post_reply_keys` table. · discourse/discourse@fad9c2b · GitHub](https://github.com/discourse/discourse/commit/fad9c2b97113ddbac16d968c38edb7ab47aa8eb9)  
Previously, we couldn’t delete `email_logs` with a `reply_key` because it would break the reply to feature. To overcome this limitation, we’re storing the reply keys in a new table that contains less columns making it more light weight.

Note that I’m playing it safe here and have yet to drop the old columns from the tables so you’ll actually see an increase in disk space used for a short period of time.

[Next page](https://meta.discourse.org/t/cleaning-up-e-mail-logs/39132.md?page=2)
