dimah-s3v0.4.1
Database

Purge

Remove stale pending rows from abandoned uploads.

If a client gets a presign and never confirms, the row stays pending. Over time that clutters listings and quota counts.

purgeStalePendingObjects deletes those stale rows. Run it on a schedule — cron, queue worker, or a script.

scripts/purge-stale-pending.ts
import { AbortMultipartUploadCommand } from "@aws-sdk/client-s3";
import { purgeStalePendingObjects } from "@dimah-s3/db";
import { dimahS3Db } from "@/lib/dimah-s3-db";
import { s3Client } from "@/lib/s3-client";

const { purged } = await purgeStalePendingObjects({
  db: dimahS3Db,
  olderThanMs: 24 * 60 * 60 * 1000,
  onBeforePurge: async (objects) => {
    for (const object of objects) {
      if (!object.uploadId) continue;
      await s3Client.send(
        new AbortMultipartUploadCommand({
          Bucket: object.bucket,
          Key: object.key,
          UploadId: object.uploadId,
        }),
      );
    }
  },
});

Returns { purged } — the rows removed from the database.

By default purge only deletes database rows. Use onBeforePurge to abort open multipart uploads on S3 so orphans do not linger in the bucket.

Rows are eligible when pending and older than olderThanMs (default 24h), or past their expiresAt. If onBeforePurge throws, the batch is skipped until the next run.

Working script: examples/with-db (pnpm db:purge-stale).