Files
outline/server/queues/tasks/ExpireDocumentsInTrashByRetentionTask.ts
T
Claude 597acedf0b fix: Complete customizable document retention
Follow-up fixes to the retention feature.

The superseded CleanupDeletedDocumentsTask was still registered and ran
hourly, hard deleting documents 30 days after `deletedAt` regardless of the
team's configured period. Any retention longer than 30 days — including
infinite — had no effect.

The `destroyedAt` column was declared with a bare `@Column`. Decorator
metadata is disabled in the server build, so sequelize-typescript could not
infer its type and threw while loading the Document model.

Documents pending permanent deletion stayed readable and restorable through
`documents.info`, `documents.restore` and the MCP restore tool. They are now
treated as if they no longer exist.

Also:

- Add trash and data retention controls to workspace security settings, the
  only way to configure either was to call the API directly.
- Derive the retention tranches to process from the periods teams actually
  use, so a team holding a period that is no longer offered is not skipped.
- Bound the trash expiry task by `limit`, it previously ran an unbounded
  UPDATE across the whole documents table every hour.
- Match team preferences on JSON text rather than casting to int, so one bad
  value cannot fail the query for every other team in the batch.
- Derive the API validation schema from the same presets the UI offers.
- Send `ASC` rather than `asc` from the trash list, the sort direction was
  silently coerced back to descending.
- Move the retention badge in with the other document badges, pluralize it,
  and never show a negative day count.
- Drop the unused `Template.destroyedAt` computed, which returned a future
  date under a name meaning the opposite on the sibling model.
- Rename the migration to match its position in the sequence and the column
  it adds.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01U3kXbDxLMJesYLM5E4P6HQ
2026-08-16 14:36:32 -04:00

93 lines
2.7 KiB
TypeScript

import { Op } from "sequelize";
import { subDays } from "date-fns";
import Logger from "@server/logging/Logger";
import { Document } from "@server/models";
import type { RetentionPreference } from "@server/utils/retention";
import { teamRetentionPeriodFilter } from "@server/utils/retention";
import { TeamPreference } from "@shared/types";
import type { PartitionInfo } from "./base/BaseTask";
import { BaseTask, TaskPriority } from "./base/BaseTask";
const preference: RetentionPreference = TeamPreference.TrashRetentionDays;
export type Props = {
/** The trash retention period in days to process in this tranche. */
retentionDays: number;
/** The maximum number of documents to expire in this task. */
limit: number;
/** Partition information for distributing work. */
partition?: PartitionInfo;
};
/**
* A task that marks documents in the trash for permanent deletion based on a retention period.
*/
export default class ExpireDocumentsInTrashByRetentionTask extends BaseTask<Props> {
public async perform({ limit, partition, retentionDays }: Props) {
// Infinite retention means documents are never expired from trash.
if (retentionDays === 0) {
return;
}
Logger.debug(
"task",
`Marking upto ${limit} documents past ${retentionDays} day trash timeout as pending permanent deletion…`
);
const team = teamRetentionPeriodFilter(
preference,
retentionDays,
"document"
);
// The batch is selected before updating as Postgres does not support a limit
// on UPDATE, and an unbounded update would hold locks across the entire table.
const documents = await Document.unscoped().findAll({
attributes: ["id"],
where: {
deletedAt: {
[Op.lt]: subDays(new Date(), retentionDays),
},
destroyedAt: {
[Op.is]: null,
},
[Op.and]: [team.where],
...this.getPartitionWhereClause("id", partition),
},
replacements: team.replacements,
paranoid: false,
limit,
});
if (!documents.length) {
return;
}
// Documents that have been in the trash for longer than the retention period
// move from the trash to pending permanent deletion.
const [count] = await Document.unscoped().update(
{
destroyedAt: new Date(),
},
{
where: {
id: documents.map((document) => document.id),
destroyedAt: {
[Op.is]: null,
},
},
paranoid: false,
}
);
Logger.info("task", `Marked ${count} documents for permanent deletion`);
}
public get options() {
return {
attempts: 1,
priority: TaskPriority.Background,
};
}
}