Add a retention period setting for model experiment runs
What does this MR do and why?
Administrators can set a retention period for model experiment runs in Admin > Settings > General > Model experiments and through the application settings API.
The default 0 keeps every run (open decision 7), and the setting has no effect yet: a later MR adds the background operation that deletes old runs that no model version uses.
When an administrator turns retention on, GitLab records the time, so runs that exist on that day get the full period from that day.
Design notes
- Storage: a new
ml_settingsJSONB column onapplication_settings, with the JSON schemaapp/validators/json_schemas/application_setting_ml_settings.json. No existing JSONB column holds settings for model experiments or the model registry, sodoc/development/application_settings.mdasks for a new one. One migration adds the column and two check constraints:jsonb_typeof(ml_settings) = 'object', like20261014233244_add_o11y_settings_to_application_settings.rb, andoctet_length(ml_settings::text) <= 65536, whichspec/db/open_ended_columns_spec.rbrequires. ml_experiment_tracking_run_retention_days: integer from0to36500, default0. It is inApplicationSettingsHelper#visible_attributes, so the Admin form and the API accept it. Changing the default later needs a data migration that writes an explicit value, because existing rows store{}and the application sets the start time only on save.ml_experiment_tracking_run_retention_enabled_at: the start time of the period, in the same column. Abefore_savecallback inApplicationSettingsets it only when the period changes, so the Admin area, the API and the Rails console behave the same:0to a positive value: set to the current time.- A positive value to another positive value: kept.
- A positive value to
0: cleared. Turning retention on again starts a new full period.
- The start time is not in
visible_attributes. The Admin form and the API ignore it when a request sends it, and the API does not return it. Under the field, the Admin area shows the first deletion date: the start time plus one period. - How the later operation uses the start time: a run is eligible when the later of its
created_at, itsupdated_atand the start time is more than one period ago. The start time is one value for the instance, so the operation checksstart time + period <= nowonce and keeps its per-run scope oncreated_atandupdated_at. - Retention policies, all with
enforcing: false, removed fromspec/support/database/retention-policy-missing-allowlist.yml:ml_candidates,ml_experiments:delete_rows, 730 days, paused by theml_experiment_tracking_run_retention_daysapplication setting.ml_candidate_metrics,ml_candidate_params,ml_candidate_metadata,ml_experiment_metadata:transient_data, deleted with their run or experiment. The window matches the parent table, liketodo_review_links.yml.ml_models,ml_model_versions,ml_model_metadata,ml_model_version_metadata:indefinite_retention. Users manage these registry entries.
config/application_setting_columns/ml_settings.ymlsetsclusterwide: false, the default that the application settings guide recommends.- No feature flag: nothing reads the setting yet, and the default
0changes nothing. Rollback is a revert. - Not in this MR: the background operation and its cron entry, the feature flag around it, any index, the user and administrator docs, the deprecation entry, and the in-product alert.
References
- Related to https://gitlab.com/gitlab-org/gitlab/-/work_items/636997
- Data retention policy framework: https://docs.gitlab.com/development/database/database_growth_control/data_retention_policy/
- Application settings guide: https://docs.gitlab.com/development/application_settings/
Screenshots or screen recordings
New Model experiments section on Admin > Settings > General, after Account and limit. Master has no such section.
| Retention off (default) | Retention on |
|---|---|
![]() |
![]() |
How the screenshots were made
A throwaway view spec rendered the section with the compiled CSS, and headless Chrome took the screenshots, so the page chrome around the section is missing.
How to set up and validate locally
- Run
bin/rails db:migrate. - Sign in as an administrator and go to
http://gdk.test:3000/admin/application_settings/general. - Expand Model experiments. Expect: Run retention period (days) is
0, with no deletion date. - Set the period to
730and select Save changes. Expect: "Deletion starts on <today plus 730 days> at the earliest." appears under the field. - Set the period to
30and save. Expect: the date changes to today plus 30 days, because the start day stays the same. - Set the period to
0and save. Expect: the deletion date line disappears.
API and negative checks
# Expect "ml_experiment_tracking_run_retention_days": 730, and no start time in the response
curl --request PUT --header "PRIVATE-TOKEN: <admin token>" \
"http://gdk.test:3000/api/v4/application/settings?ml_experiment_tracking_run_retention_days=730&ml_experiment_tracking_run_retention_enabled_at=2020-01-01" \
| jq '{ml_experiment_tracking_run_retention_days, ml_experiment_tracking_run_retention_enabled_at}'
# Expect 400 Bad Request, also for 36501
curl --request PUT --header "PRIVATE-TOKEN: <admin token>" \
"http://gdk.test:3000/api/v4/application/settings?ml_experiment_tracking_run_retention_days=-1"In the Rails console, Gitlab::CurrentSettings.ml_experiment_tracking_run_retention_enabled_at returns the time of the first request, not 2020-01-01.
Migration output
The change adds one column with a constant default and two check constraints to application_settings, which has one row. It adds or changes no queries.
Up:
main: == 20261113150059 AddMlSettingsToApplicationSettings: migrating ===============
main: -- transaction_open?(nil)
main: -> 0.0000s
main: -- add_column(:application_settings, :ml_settings, :jsonb, {default: {}, null: false, if_not_exists: true})
main: -> 0.2073s
main: -- transaction_open?(nil)
main: -> 0.0000s
main: -- transaction_open?(nil)
main: -> 0.0000s
main: -- execute("ALTER TABLE application_settings\nADD CONSTRAINT check_application_settings_ml_settings_is_hash\nCHECK ( (jsonb_typeof(ml_settings) = 'object') )\nNOT VALID;\n")
main: -> 0.0008s
main: -- execute("ALTER TABLE application_settings VALIDATE CONSTRAINT check_application_settings_ml_settings_is_hash;")
main: -> 0.0005s
main: -- transaction_open?(nil)
main: -> 0.0000s
main: -- transaction_open?(nil)
main: -> 0.0000s
main: -- execute("ALTER TABLE application_settings\nADD CONSTRAINT check_application_settings_ml_settings_size\nCHECK ( octet_length(ml_settings::text) <= 65536 )\nNOT VALID;\n")
main: -> 0.0005s
main: -- execute("ALTER TABLE application_settings VALIDATE CONSTRAINT check_application_settings_ml_settings_size;")
main: -> 0.0005s
main: == 20261113150059 AddMlSettingsToApplicationSettings: migrated (0.2309s) ======Down:
main: == 20261113150059 AddMlSettingsToApplicationSettings: reverting ===============
main: -- transaction_open?(nil)
main: -> 0.0000s
main: -- transaction_open?(nil)
main: -> 0.0000s
main: -- execute(" ALTER TABLE application_settings\n DROP CONSTRAINT IF EXISTS check_application_settings_ml_settings_size\n")
main: -> 0.0011s
main: -- transaction_open?(nil)
main: -> 0.0000s
main: -- transaction_open?(nil)
main: -> 0.0000s
main: -- execute(" ALTER TABLE application_settings\n DROP CONSTRAINT IF EXISTS check_application_settings_ml_settings_is_hash\n")
main: -> 0.0005s
main: -- transaction_open?(nil)
main: -> 0.0000s
main: -- remove_column(:application_settings, :ml_settings, {if_exists: true})
main: -> 0.1949s
main: == 20261113150059 AddMlSettingsToApplicationSettings: reverted (0.2101s) ======MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

