Overview
Pipelines-as-Code can automatically clean up old PipelineRuns to prevent namespace clutter and resource exhaustion. The cleanup mechanism keeps only a specified number of recent PipelineRuns and removes older ones after successful completion.Configuration
Annotation-Based Cleanup
Add themax-keep-runs annotation to your PipelineRun:
Maximum number of PipelineRuns to retain for this pipeline definition.
- Type: String representation of an integer
- Default: No automatic cleanup
- Scope: Per PipelineRun definition (not per repository)
- Trigger: Cleanup runs after each successful PipelineRun completion
Global Default Configuration
Set a cluster-wide default in thepipelines-as-code ConfigMap:
Annotation-level settings override the global ConfigMap default.
How Cleanup Works
Trigger Conditions
Cleanup is triggered when:- A PipelineRun completes successfully (Succeeded condition)
- The
max-keep-runsannotation is set (or global default exists) - The number of completed PipelineRuns exceeds the limit
Cleanup Behavior
- Counts Completed Runs: PAC counts PipelineRuns with the same base name
- Sorts by Creation Time: Older PipelineRuns are identified
- Skips Running/Pending: PipelineRuns that are still running or pending are preserved
- Includes Unknown: PipelineRuns with Unknown status are included in cleanup
- Deletes Oldest: Excess PipelineRuns beyond the limit are deleted
Retention Scope
Cleanup applies per PipelineRun definition, not per repository:Examples
Basic Retention Policy
Basic Retention Policy
- First 5 PRs: All PipelineRuns are kept
- 6th PR: Oldest PipelineRun is deleted after completion
- Ongoing: Always maintains exactly 5 completed runs
Different Retention by Pipeline Type
Different Retention by Pipeline Type
Environment-Specific Retention
Environment-Specific Retention
Integration with Concurrency Limit
Integration with Concurrency Limit
- Maximum 3 PipelineRuns execute concurrently
- Up to 10 completed PipelineRuns are retained
- Total PipelineRuns in namespace: 3 (running) + 10 (completed) = 13 maximum
What Gets Cleaned Up
Included in Cleanup
- Completed PipelineRuns: Succeeded or Failed status
- Unknown Status: PipelineRuns with Unknown status
- Associated Resources: TaskRuns, Pods, and PersistentVolumeClaims owned by the PipelineRun
- Generated Secrets:
pac-gitauth-*secrets with ownerReference to the PipelineRun
Excluded from Cleanup
- Running PipelineRuns: Currently executing
- Pending PipelineRuns: Queued but not yet started
- Manual Runs: PipelineRuns without the annotation (unless global default is set)
- Other Repositories: Only PipelineRuns from the same pipeline definition
Cleanup uses Kubernetes ownerReferences, so when a PipelineRun is deleted, all its child resources (TaskRuns, Pods, Secrets) are automatically deleted.
Monitoring Cleanup
View Recent PipelineRuns
Check Cleanup Activity
PAC logs cleanup operations:Prometheus Metrics
If Prometheus monitoring is enabled, track cleanup metrics:Use Cases
High-Frequency CI/CD
High-Frequency CI/CD
- Keeps only the 3 most recent runs
- Prevents namespace overflow
- Reduces storage costs
Audit and Compliance
Audit and Compliance
- Retains extensive build history
- Supports audit trails
- May require additional storage capacity
Resource-Constrained Clusters
Resource-Constrained Clusters
- Keeps only the latest run
- Minimizes storage usage
- Immediate cleanup after each run
Multi-Environment Pipelines
Multi-Environment Pipelines
Storage Impact
Each PipelineRun creates several Kubernetes resources:- PipelineRun object: ~10-50 KB
- TaskRun objects: ~10-30 KB each (multiple per pipeline)
- Pod objects: ~10-20 KB each
- Logs: 1-100 MB per run (depending on verbosity)
- PVCs: 1-10 GB if using workspace volumes
Calculation Example
Scenario: 100 commits per day, 3 PipelineRuns per commitBest Practices
- Start Conservative: Begin with low retention (5-10 runs) and increase if needed
-
Different Policies for Different Pipelines:
- Frequent/fast pipelines (lint): 2-3 runs
- Standard CI/CD: 10-20 runs
- Release/deployment: 50+ runs
- Consider Storage Costs: Calculate storage impact before setting high retention
-
Match Retention to Value:
- Keep more runs for critical pipelines
- Keep fewer runs for exploratory/test pipelines
- Monitor Cleanup: Check logs to ensure cleanup is working as expected
- Export Historical Data: If you need long-term retention, export PipelineRun data to external storage
- Set Global Default: Establish a reasonable cluster-wide default to prevent unbounded growth
- Document Policies: Explain retention policies in your project’s CI/CD documentation
Troubleshooting
PipelineRuns Not Being Cleaned Up
PipelineRuns Not Being Cleaned Up
Possible causes:
- Annotation not set or has typo (
max-keep-runsnotmax-keep-run) - PipelineRuns are not completing successfully
- Still under the retention limit
- Global ConfigMap setting is higher than expected
Too Many PipelineRuns Deleted
Too Many PipelineRuns Deleted
Cause: Retention limit set too lowSolution: Increase
max-keep-runs value:Namespace Still Growing
Namespace Still Growing
Possible causes:
- Multiple PipelineRun definitions without retention
- Long-running PipelineRuns not completing
- PVCs not cleaned up (not owned by PipelineRun)
- Other resources in namespace
Critical Run Was Deleted
Critical Run Was Deleted
Prevention: Increase retention for important pipelinesRecovery:
- Check Repository CR status (keeps last 5 run statuses)
- Review Git provider status checks/comments
- Restore from backup if available
- Re-run the pipeline if needed
Advanced Configuration
Custom Retention Logic
For advanced scenarios, you can implement custom cleanup logic using Tekton’sPipelineRun lifecycle hooks:
See Also
- Repository CRD - Repository configuration reference
- Concurrency - Managing concurrent PipelineRuns
- Settings - Global PAC configuration options
- Running PipelineRuns - PipelineRun lifecycle and management