Data retention
Many privacy policies promise that some data is deleted after a while: messages after 90 days, logs after a year. --retain makes that promise true for a resource. Every night, rows older than the window are removed for good, and so are the files they point at.
Generate one
$grit generate resource ChatMessage \--fields "body:text,attachment:file" \--retain 90d$grit migrate
In a --from file, the same thing is retain: 90d.
Writing a window
A window is a whole number of days, weeks or hours:
| Value | Rows are kept for |
|---|---|
90d | 90 days |
2w | 14 days |
36h | 36 hours |
Months and years are refused rather than guessed. A month is 28 to 31 days, and a policy that says "6 months" and deletes at 180 days has deleted early, which is the one way a retention promise must not be wrong. Write 180d or 365d and the window means exactly what the policy says. Zero, negative and windows over 100 years are refused too: to keep rows for ever, leave --retain off.
What you get
The window is declared in the model's own file:
func init() { retention.Register(&ChatMessage{}, 90*24*time.Hour) }
That is the whole policy. grit remove resource ChatMessage deletes the model and the policy with it, and changing the number changes the window. A window that is not positive stops the API from starting, because a window of zero would delete every row on the next run.
| When (UTC) | Job | What it does |
|---|---|---|
| 03:00 | retention:sweep | Removes every row whose created_at is older than its resource's window, 500 at a time, and marks the files those rows pointed at as no longer used. |
| 03:15 | uploads:cleanup_orphans | The existing orphan sweep. It deletes the files the retention sweep released, with their thumbnails and the kept original, so they usually go the same night. |
Both are on the admin's scheduled jobs list, and the sweep logs what it removed:
Retention sweep: removed 1 chat_messages row(s) past their windowOrphan upload cleanup complete, removed 1 uploads
How rows are removed
- For good. A row is hard-deleted, not moved to the Trash. A row that is already in the Trash goes too: the policy is about when the row was created, not when somebody deleted it.
- Files through the orphan sweep. The sweep releases a removed row's uploads rather than deleting them itself, the same way purging the Trash does. The orphan sweep already checks that no other record still points at a file and already deletes every size of an image, so that logic lives in one place. A right-to-erasure request is the case that cannot wait, and it deletes files immediately.
- One resource at a time. If one resource cannot be swept, the others still are, and the job reports the failure so it shows on the jobs page.
Existing projects
grit upgrade adds the sweep and schedules it. If you pass --retain to a project that has not been upgraded yet, the generator says that nothing will remove the rows until you run it.
--retain cannot be combined with --append-only, whose rows can never be deleted. It cannot be combined with --tree or --items yet either: the database refuses to delete a row that other rows still point at, and a tree's parent is always older than its children. The same applies to a retained resource that another resource belongs to: a row still referenced is refused, the job reports it, and the rest of the sweep carries on.