dataiotech Labs
dbaguidedbaguideDBAdataxtoolsdataopsphere
Scripts Library/mongodb/#95
Script #95
mongodb
Maintenanceread-onlyMin Version: 4.0

List MongoDB TTL Indexes and Retention Periods

Shows TTL indexes and retention periods per collection. Used to audit data retention policies and unexpected deletions.

#ttl#expire#retention
mongodb · javascript
const out = [];
db.getCollectionInfos({ type: "collection" }).forEach(c => {
  db.getCollection(c.name).getIndexes().forEach(i => {
    if (i.expireAfterSeconds !== undefined) {
      out.push({ collection: c.name, index: i.name, key: JSON.stringify(i.key),
                 expire_days: +(i.expireAfterSeconds / 86400).toFixed(2) });
    }
  });
});
console.table(out);
Operational Notes & Remediation Guidance

Verify in a staging environment prior to running in production. This is a read-only diagnostic query; it does not acquire exclusive locks.

Production Safety & Execution Standards
  • This script is designed for read-only diagnostics; it does not alter schema or modify data directly.
  • Prior to executing on production systems under high load, verify the target database version (Min. Version: 4.0).
  • Exercise caution with mutating actions (e.g., terminating sessions or dropping indexes) referenced in the notes section.