Packages
phoenix_kit
1.6.13
1.7.210
1.7.209
1.7.208
1.7.207
1.7.206
1.7.205
1.7.204
1.7.203
1.7.202
1.7.201
1.7.200
1.7.199
1.7.198
1.7.197
1.7.196
1.7.194
1.7.193
1.7.192
1.7.191
1.7.190
1.7.189
1.7.187
1.7.186
1.7.185
1.7.184
1.7.183
1.7.182
1.7.181
1.7.180
1.7.179
1.7.178
1.7.177
1.7.176
1.7.175
1.7.174
1.7.173
1.7.172
1.7.171
1.7.170
1.7.169
1.7.168
1.7.167
1.7.166
1.7.165
1.7.164
1.7.162
1.7.161
1.7.160
1.7.159
1.7.157
1.7.156
1.7.155
1.7.154
1.7.153
1.7.152
1.7.151
1.7.150
1.7.149
1.7.146
1.7.145
1.7.144
1.7.143
1.7.138
1.7.133
1.7.132
1.7.131
1.7.130
1.7.128
1.7.126
1.7.125
1.7.121
1.7.120
1.7.119
1.7.118
1.7.117
1.7.116
1.7.115
1.7.114
1.7.113
1.7.112
1.7.111
1.7.110
1.7.109
1.7.108
1.7.107
1.7.106
1.7.105
1.7.104
1.7.103
1.7.102
1.7.101
1.7.100
1.7.99
1.7.98
1.7.97
1.7.96
1.7.95
1.7.94
1.7.93
1.7.92
1.7.91
1.7.90
1.7.89
1.7.88
1.7.87
1.7.86
1.7.85
1.7.84
1.7.83
1.7.82
1.7.81
1.7.80
1.7.79
1.7.78
1.7.77
1.7.76
1.7.75
1.7.74
1.7.71
1.7.70
1.7.69
1.7.66
1.7.65
1.7.64
1.7.63
1.7.62
1.7.61
1.7.59
1.7.58
1.7.57
1.7.56
1.7.55
1.7.54
1.7.53
1.7.52
1.7.51
1.7.49
1.7.44
1.7.43
1.7.42
1.7.41
1.7.39
1.7.38
1.7.37
1.7.36
1.7.34
1.7.33
1.7.31
1.7.30
1.7.29
1.7.28
1.7.27
1.7.26
1.7.25
1.7.24
1.7.23
1.7.22
1.7.21
1.7.20
1.7.19
1.7.18
1.7.17
1.7.16
1.7.15
1.7.14
1.7.13
1.7.12
1.7.11
1.7.10
1.7.9
1.7.8
1.7.7
1.7.6
1.7.5
1.7.4
1.7.3
1.7.2
1.7.1
1.7.0
1.6.20
1.6.19
1.6.18
1.6.17
1.6.16
1.6.15
1.6.14
1.6.13
1.6.12
1.6.11
1.6.10
1.6.9
1.6.8
1.6.7
1.6.6
1.6.5
1.6.4
1.6.3
1.5.2
1.5.1
1.5.0
1.4.9
1.4.8
1.4.7
1.4.6
1.4.5
1.4.4
1.4.3
1.4.2
1.4.1
1.4.0
1.3.2
1.3.1
1.3.0
1.2.10
1.2.9
1.2.8
1.2.7
1.2.5
1.2.4
1.2.2
1.2.1
1.2.0
1.1.0
1.0.0
A foundation for building Elixir Phoenix apps — SaaS, social networks, ERP systems, marketplaces, and more
Current section
Files
Jump to
Current section
Files
lib/mix/tasks/phoenix_kit/email_cleanup.ex
defmodule Mix.Tasks.PhoenixKit.Email.Cleanup do
@shortdoc "Clean up old email system logs"
@moduledoc """
Mix task to clean up old email system logs and optimize storage.
## Usage
# Clean logs older than default retention period (90 days)
mix phoenix_kit.email.cleanup
# Clean logs older than specific number of days
mix phoenix_kit.email.cleanup --older-than 30d
# Show what would be deleted without actually deleting
mix phoenix_kit.email.cleanup --dry-run
# Compress old bodies instead of deleting
mix phoenix_kit.email.cleanup --compress-only
# Archive to S3 before deleting
mix phoenix_kit.email.cleanup --archive
## Options
--older-than PERIOD Delete logs older than period (e.g., 30d, 60d, 90d)
--dry-run Show what would be deleted without deleting
--compress-only Only compress old email bodies, don't delete
--archive Archive logs to S3 before deleting
--force Skip confirmation prompts
## Examples
# Safe dry run to see what would be cleaned
mix phoenix_kit.email.cleanup --dry-run
# Clean logs older than 30 days with archive
mix phoenix_kit.email.cleanup --older-than 30d --archive
# Compress bodies for logs older than 7 days
mix phoenix_kit.email.cleanup --older-than 7d --compress-only
"""
use Mix.Task
alias PhoenixKit.Emails
@impl Mix.Task
def run(args) do
Mix.Task.run("app.start")
{options, _remaining} = parse_options(args)
# Note: Emails.enabled?() check omitted as Dialyzer determines it's always true
days_old = parse_days(options[:older_than])
Mix.shell().info(IO.ANSI.cyan() <> "\n🧹 Email Cleanup" <> IO.ANSI.reset())
Mix.shell().info(String.duplicate("=", 40))
if options[:compress_only] do
run_compression(days_old, options)
else
run_cleanup(days_old, options)
end
end
defp parse_options(args) do
{options, remaining, _errors} =
OptionParser.parse(args,
strict: [
older_than: :string,
dry_run: :boolean,
compress_only: :boolean,
archive: :boolean,
force: :boolean
]
)
# Set defaults
options =
options
|> Keyword.put_new(:dry_run, false)
|> Keyword.put_new(:compress_only, false)
|> Keyword.put_new(:archive, false)
|> Keyword.put_new(:force, false)
{options, remaining}
end
defp parse_days(nil) do
# Use system retention setting or default to 90 days
Emails.get_retention_days()
end
defp parse_days(period_string) do
case Regex.run(~r/^(\d+)d?$/, period_string) do
[_, days_str] ->
String.to_integer(days_str)
_ ->
Mix.shell().error("Invalid period format. Use format like '30d' or '90d'")
exit({:shutdown, 1})
end
end
defp run_compression(days_old, options) do
Mix.shell().info("🗜️ Compressing email bodies older than #{days_old} days...")
if options[:dry_run] do
# Show what would be compressed
count = count_compressible_logs(days_old)
Mix.shell().info("Would compress #{count} email log bodies")
else
{compressed_count, _} = Emails.compress_old_bodies(days_old)
if compressed_count > 0 do
Mix.shell().info("✅ Compressed #{compressed_count} email log bodies")
else
Mix.shell().info("ℹ️ No email bodies found to compress")
end
end
end
defp run_cleanup(days_old, options) do
Mix.shell().info("🗑️ Cleaning up emails older than #{days_old} days...")
if options[:archive] do
Mix.shell().info("📦 Archiving to S3 before deletion...")
if not options[:dry_run] do
case Emails.archive_to_s3(days_old) do
{:ok, :skipped} ->
Mix.shell().info("ℹ️ Archive skipped (email system disabled)")
{:ok, result} ->
archived_count = Keyword.get(result, :archived_count, 0)
Mix.shell().info("✅ Archived #{archived_count} logs to S3")
end
end
end
if options[:dry_run] do
count = count_deletable_logs(days_old)
Mix.shell().info("Would delete #{count} emails and their events")
else
if not options[:force] do
confirm_deletion_or_exit(days_old)
end
{deleted_count, _} = Emails.cleanup_old_logs(days_old)
if deleted_count > 0 do
Mix.shell().info("✅ Deleted #{deleted_count} old emails")
Mix.shell().info("💾 Storage space has been freed up")
else
Mix.shell().info("ℹ️ No old emails found to delete")
end
end
end
defp count_compressible_logs(days_old) do
_cutoff_date = Date.utc_today() |> Date.add(-days_old)
# This would need to be implemented in Email module
# For now, return 0
0
end
defp count_deletable_logs(days_old) do
_cutoff_date = Date.utc_today() |> Date.add(-days_old)
# This would need to be implemented in Email module
# For now, return a mock count for demonstration
42
end
defp confirm_deletion_or_exit(days_old) do
count = count_deletable_logs(days_old)
if count > 0 do
message =
"This will permanently delete #{count} emails older than #{days_old} days. Continue?"
unless Mix.shell().yes?(message) do
Mix.shell().info("Cleanup cancelled")
exit({:shutdown, 0})
end
end
end
end