| name | archiving-databases |
| description | Process use when you need to archive historical database records to reduce primary database size.
This skill automates moving old data to archive tables or cold storage (S3, Azure Blob, GCS).
Trigger with phrases like "archive old database records", "implement data retention policy",
"move historical data to cold storage", or "reduce database size with archival".
|
| allowed-tools | Read, Write, Edit, Grep, Glob, Bash(psql:*), Bash(mysql:*), Bash(aws:s3:*), Bash(az:storage:*) |
| version | 1.27.0 |
| author | Jeremy Longshore <jeremy@intentsolutions.io> |
| license | MIT |
| tags | ["database","azure","archiving-databases"] |
| compatibility | Designed for Claude Code, also compatible with Codex and OpenClaw |
Database Archival System
Overview
Implement automated data archival pipelines that move historical records from primary database tables to archive storage (archive tables, S3, Azure Blob, or GCS) based on age, status, or access frequency criteria.
Prerequisites
- Database credentials with SELECT, INSERT, and DELETE permissions on source and archive tables
- Cloud storage credentials (AWS S3, Azure Blob, or GCS) if archiving to cold storage
psql or mysql CLI for executing archival queries
aws s3, az storage, or gsutil CLI for cloud storage uploads
- Understanding of data retention requirements and compliance policies (GDPR, HIPAA, SOX)
- Current table sizes:
SELECT pg_size_pretty(pg_total_relation_size('table_name')) to identify archival candidates
Instructions
-
Identify archival candidates by finding large tables with time-based data:
SELECT relname, n_live_tup, pg_size_pretty(pg_total_relation_size(relid)) FROM pg_stat_user_tables ORDER BY pg_total_relation_size(relid) DESC LIMIT 10
- Focus on tables where historical data is rarely queried: logs, audit trails, events, old orders, expired sessions
-
Define archival criteria for each table:
- Age-based: Records older than N days/months (
WHERE created_at < NOW() - INTERVAL '1 year')
- Status-based: Records in terminal state (
WHERE status IN ('completed', 'cancelled', 'expired'))
- Combined: Old AND terminal (
WHERE created_at < NOW() - INTERVAL '6 months' AND status = 'completed')
- Calculate the expected volume:
SELECT COUNT(*), pg_size_pretty(pg_column_size(t.*)) FROM table_name t WHERE <criteria>
-
Handle referential integrity by archiving in dependency order:
- Archive child records first (order_items before orders)
- For tables with active foreign key references, verify no active records reference the candidates:
SELECT COUNT(*) FROM active_child WHERE parent_id IN (SELECT id FROM parent WHERE <archive_criteria>)
- Option: cascade archive by archiving parent and all descendants together
-
Create archive destination tables matching the source schema plus metadata columns: