Storage retention and cleanup
Plans complete storage cleanup without confusing logical deletion with physical removal, retained versions or protected records.
retention-storage-cleanup
Team members using this skill
When is it useful?
Use it when abandoned uploads and orphaned derivatives accumulate, or you need a reviewable deletion plan under existing retention rules.
What does it help you do?
- Inventories originals, derivatives, staging copies and unfinished uploads.
- Uses grace periods and active-reference checks before purge.
- Accounts for versions, retention holds and quota adjustments honestly.
Try asking this
Prepare an orphan-file cleanup preview that excludes active uploads and retained records. Include a recently started upload and an old unreferenced thumbnail in the preview. Explain each candidate's age, references and hold status before deletion is approved.
Detailed documentation
Read the skill’s original instructions, examples and checks in their authored language.
Show less
Retention & Storage Cleanup
Without deliberate lifecycle cleanup, storage costs grow unchecked from abandoned multipart uploads, failed transcode artifacts, orphaned thumbnails and deleted records whose underlying blobs were left behind. Equally critical: an overly aggressive cleanup script can delete in-flight uploads or violate backup/legal retention rules.
1. Cascading Deletion Completeness
When a user or workspace deletes a file (or an entire project/account is deleted), track every artifact that must be cleaned up:
- Primary storage object (
storage_key). - All derived variants (
file_derivatives: thumbnails, responsive sizes, PDF previews, audio waveforms, and entire HLS/DASH segment prefixes). - Quarantine / staging copies (if deletion happens before promotion).
- Active multipart upload sessions (
AbortMultipartUploadso unmerged parts are freed). - Share links & CDN cache: invalidate
file_sharesimmediately and purge edge cache keys if the asset was cached on a CDN. - Quota accounting: decrement
used_bytes(or releasereserved_bytes) atomically once deletion is committed.
2. Two-Phase Deletion & Grace Windows
Never delete storage bytes synchronously inside a user-facing DELETE database transaction (if the DB rolls back after DeleteObject, data is permanently lost):
- Phase 1 — Logical Soft-Delete:
- Set
deleted_at = NOW(),purge_after = NOW() + grace_period(e.g., immediate for temporary drafts, or 7–30 days for recoverable trash / accidental-deletion recovery). - Revoke all read/download/share access immediately when
deleted_at IS NOT NULL.
- Set
- Phase 2 — Asynchronous Physical Purge:
- A background worker selects rows where
status = 'soft_deleted' AND purge_after <= NOW()(and no legal/compliance hold applies), deletes all derivative and primary storage objects, and marksstatus = 'purged',purged_at = NOW().
- A background worker selects rows where
3. Safe Orphan Sweeping (Avoiding In-Flight Deletion)
When sweeping for orphaned files or abandoned uploads:
- Enforce a generous minimum age threshold (Safety Grace Window):
- Never delete an unreferenced storage object or
'uploading'session that is younger than the maximum possible upload/processing duration (e.g., only sweep'uploading'sessions whereexpires_at < NOW() - INTERVAL '2 hours', and only sweep unreferenced storage blobs older than24–72 hours).
- Never delete an unreferenced storage object or
- Verify zero active references before deleting:
- If content-addressed storage or deduplication is used, verify that zero active
filesrows reference the blob hash (with a lock or reference counter) before deleting the underlying object.
- If content-addressed storage or deduplication is used, verify that zero active
- Dry-run & batch rate limiting:
- Build mass-cleanup jobs with a
--dry-runmode that logs candidate count and total bytes first, and process deletions in bounded batches (DeleteObjectsup to 1,000 keys) with audit logging.
- Build mass-cleanup jobs with a
4. Object Versioning, Backups & Honest Deletion Claims
- Check whether S3/Blob Object Versioning, Object Lock (WORM) or automated snapshot backups are enabled on the bucket.
- In a versioned bucket, a standard
DeleteObjectonly inserts a Delete Marker while keeping prior versions billable and recoverable unless aVersionIddelete or lifecycle expiration rule is configured. Document the actual retention behavior honestly.
Included files 1
SKILL.md
