We value your privacy

This site uses cookies to improve your browsing experience, analyze site traffic, and show personalized content. See our Privacy Policy.

  1. MooseStack
  2. Moose OLAP
  3. TTL (Time-to-Live) for ClickHouse Tables

TTL (Time-to-Live) for ClickHouse Tables

Moose lets you declare ClickHouse TTL directly in your data model:

  • Table-level TTL via the ttl option on OlapTable config
  • Column-level TTL via ClickHouseTTL on individual fields

When to use TTL

  • Automatically expire old rows to control storage cost
  • Mask or drop sensitive columns earlier than the full row expiry

Configuration

TTLExample.ts
import { OlapTable, ClickHouseEngines, Key, DateTime, ClickHouseTTL } from "@514labs/moose-lib"; interface Event {  id: Key<string>;  timestamp: DateTime;  email: string & ClickHouseTTL<"timestamp + INTERVAL 30 DAY">; // column TTL} export const Events = new OlapTable<Event>("Events", {  engine: ClickHouseEngines.MergeTree,  orderByFields: ["id", "timestamp"],  // Provide the ClickHouse TTL expression without the leading 'TTL'  ttl: "timestamp + INTERVAL 90 DAY DELETE", // table TTL});

Notes

  • Expressions must be valid ClickHouse TTL expressions, but do not include the leading TTL keyword.
  • Column TTLs are independent from the table TTL and can be used together.
  • Moose will apply TTL changes via migrations using ALTER TABLE ... MODIFY TTL and MODIFY COLUMN ... TTL.

Related

  • See Modeling Tables for defining your schema
  • See Applying Migrations to roll out TTL changes

On this page

When to use TTLConfigurationNotesRelated
FiveonefourFiveonefour
Fiveonefour Docs
MooseStackHostingTemplatesGuides
Release Notes
Source531
  • Overview
Build a New App
  • 5 Minute Quickstart
  • Browse Templates
  • Existing ClickHouse
Add to Existing App
  • Next.js
  • Fastify
Fundamentals
  • Moose Runtime
  • MooseDev MCP
  • Language Server
  • Data Modeling
Moose Modules
  • Moose OLAP
    • Data Modeling
    • Tables
    • Views
    • Materialized Views
    • Materialized Columns
    • External Data & Introspection
    • External Tables
    • Introspecting Tables
    • Data Access
    • Inserting Data
    • Reading Data
    • Performance & Optimization
    • Schema Optimization
    • Secondary & Data-skipping Indexes
    • TTL (Time-to-Live)
    • Schema Versioning
  • Moose Streaming
  • Moose Workflows
  • Moose APIs & Web Apps
Deployment & Lifecycle
  • Moose Dev
  • Moose Migrate
  • Moose Deploy
Reference
  • API Reference
  • Data Types
  • Table Engines
  • CLI
  • Configuration
  • Observability Metrics
  • Help
  • Release Notes
Contribution
  • Documentation
  • Framework
TTLExample.ts
import { OlapTable, ClickHouseEngines, Key, DateTime, ClickHouseTTL } from "@514labs/moose-lib"; interface Event {  id: Key<string>;  timestamp: DateTime;  email: string & ClickHouseTTL<"timestamp + INTERVAL 30 DAY">; // column TTL} export const Events = new OlapTable<Event>("Events", {  engine: ClickHouseEngines.MergeTree,  orderByFields: ["id", "timestamp"],  // Provide the ClickHouse TTL expression without the leading 'TTL'  ttl: "timestamp + INTERVAL 90 DAY DELETE", // table TTL});