0
0
Jenkinsdevops~10 mins

Artifact retention policies in Jenkins - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Artifact retention policies
Build completes
Store artifacts
Check retention policy
Is artifact older than limit?
NoKeep artifact
Yes
Delete artifact
End
This flow shows how Jenkins stores build artifacts, then checks if they are older than the retention policy allows, deleting them if so.
Execution Sample
Jenkins
pipeline {
  agent any
  options {
    buildDiscarder(logRotator(numToKeepStr: '3', artifactNumToKeepStr: '2'))
  }
  stages {
    stage('Build') { steps { echo 'Building...' } }
  }
}
This Jenkins pipeline keeps only the last 3 builds and retains artifacts only for the last 2 builds.
Process Table
StepBuild NumberArtifacts StoredRetention Policy CheckAction
1Build #1StoredNo older builds to deleteKeep artifacts
2Build #2StoredNo older builds to deleteKeep artifacts
3Build #3StoredBuild #1 artifacts older than 2 buildsDelete Build #1 artifacts
4Build #4StoredBuild #2 artifacts older than 2 buildsDelete Build #2 artifacts
5Build #5StoredBuild #3 artifacts older than 2 buildsDelete Build #3 artifacts
6Build #6StoredBuild #4 artifacts older than 2 buildsDelete Build #4 artifacts
7Build #7StoredBuild #5 artifacts older than 2 buildsDelete Build #5 artifacts
8Build #8StoredBuild #6 artifacts older than 2 buildsDelete Build #6 artifacts
9Build #9StoredBuild #7 artifacts older than 2 buildsDelete Build #7 artifacts
10Build #10StoredBuild #8 artifacts older than 2 buildsDelete Build #8 artifacts
💡 After Build #10, only artifacts from builds #9 and #10 are kept as per retention policy.
Status Tracker
Build NumberArtifacts StoredArtifacts Deleted
#1YesNo
#2YesNo
#3YesNo
#4YesNo
#5YesNo
#6YesNo
#7YesNo
#8YesNo
#9YesNo
#10YesNo
Key Moments - 3 Insights
Why are artifacts from Build #1 deleted after Build #3?
Because the retention policy keeps artifacts only for the last 2 builds, so after Build #3, Build #1 is older than 2 builds and its artifacts are deleted (see execution_table row 3).
Does the retention policy delete the entire build or just the artifacts?
It deletes only the artifacts, not the entire build record. The build history remains but artifacts older than the limit are removed (see execution_table actions).
What happens if the retention policy is set to keep 0 artifacts?
No artifacts are kept after each build completes; all previous artifacts are deleted immediately (not shown in table but implied by policy).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which build number are artifacts from Build #2 deleted?
ABuild #4
BBuild #5
CBuild #3
DBuild #6
💡 Hint
Check the 'Retention Policy Check' and 'Action' columns for Build #4 in the execution_table.
According to the variable tracker, are artifacts from Build #5 stored or deleted after Build #7?
ADeleted
BStored
CPartially stored
DUnknown
💡 Hint
Look at the 'Artifacts Stored' and 'Artifacts Deleted' columns for Build #5 in variable_tracker.
If the retention policy changed to keep 3 artifacts, what would happen at Build #5?
AArtifacts from Build #2 would be deleted
BArtifacts from Build #1 would be deleted
CNo artifacts would be deleted yet
DAll artifacts would be deleted
💡 Hint
Consider the retention policy effect on artifact age in execution_table rows around Build #5.
Concept Snapshot
Jenkins artifact retention policies control how many build artifacts to keep.
Use 'buildDiscarder' with 'logRotator' to set limits.
Example: keep last 3 builds, artifacts for last 2 builds.
Older artifacts are deleted automatically after new builds.
This saves disk space and keeps recent artifacts accessible.
Full Transcript
This visual execution shows how Jenkins handles artifact retention. After each build, Jenkins stores artifacts. It then checks if artifacts are older than the retention policy allows. If yes, Jenkins deletes those old artifacts. For example, with a policy to keep artifacts for only the last 2 builds, artifacts from Build #1 are deleted after Build #3 completes. The execution table tracks each build's artifacts storage and deletion actions. The variable tracker shows which builds have artifacts stored or deleted over time. Key moments clarify common confusions like why only artifacts are deleted, not builds, and how the retention policy affects artifact lifespan. The quiz tests understanding of when artifacts are deleted and how changing the policy affects retention. The snapshot summarizes how to configure and what the policy does in Jenkins pipelines.