Artifact object storage size reconciliation
[[TOC]]
Contact Information
Section titled “Contact Information”- Group: Verify:Pipeline Execution
- Slack: #g_pipeline-execution
When to use this runbook
Section titled “When to use this runbook”Use this when the reported size of an artifacts object storage bucket (AWS CloudWatch / Grafana / S3 Storage Lens) is much larger than the artifact size GitLab reports (project statistics, REST/GraphQL API, or the database), and you need to find where the extra bytes are.
Why the numbers can differ
Section titled “Why the numbers can differ”The bucket size counts every byte S3 holds; GitLab’s figures count only rows in
ci_job_artifacts. Known sources of a gap, roughly in the order to check:
- Noncurrent object versions (versioned buckets): every expired or overwritten artifact leaves an old version until the lifecycle rule deletes it. High churn on a large bucket can produce a gap larger than the tracked artifact total.
- Incomplete multipart uploads: failed or abandoned artifact uploads leave
parts behind when the bucket has no
AbortIncompleteMultipartUploadrule. - Orphaned job artifacts: objects under
@final/whose database row is gone. Usually a small share of the gap. - Delete markers and untracked prefixes such as
tmp/uploads/andtmp/builds/. - Pipeline artifacts (
pipelines/<id>/artifacts/<id>/...): tracked inci_pipeline_artifacts, notci_job_artifacts, so aci_job_artifacts-only comparison misses them (they are usually small).
Also rule out measurement errors first: the GraphQL query
group { projects(includeSubgroups: true) { ... statistics { buildArtifactsSize } } }
counts a subgroup project’s statistics once per ancestor group when run per
group, inflating the customer-side figure.
Prerequisites
Section titled “Prerequisites”- The bucket name(s) and AWS account access for S3 API calls (
aws s3api). - Grafana/CloudWatch access for
aws_s3_bucket_size_bytes_averageandaws_s3_number_of_objects_averageper bucket. - A GitLab Rails console or toolbox pod on the instance (break-glass on Dedicated; follow the tenant’s change process).
- For the size split: S3 Storage Lens advanced metrics (about a day to fill in) or an S3 Inventory report with the “all versions” option (up to 48 hours for the first report).
Step 1: Compare bucket totals with the database
Section titled “Step 1: Compare bucket totals with the database”Get the bucket totals from Prometheus/Grafana:
aws_s3_bucket_size_bytes_average{bucket_name=~".*-artifacts"}aws_s3_number_of_objects_average{bucket_name=~".*-artifacts"}Note that aws_s3_bucket_size_bytes_average without a StorageType dimension
sums all storage classes and version states; it is not directly comparable to
the database figure until step 2 splits it.
Get the tracked job artifact totals from the database (read-only):
SELECT COUNT(*) AS object_count, SUM(size) AS total_bytes, ROUND(SUM(size) / 1024.0 / 1024.0 / 1024.0, 2) AS total_gbFROM p_ci_job_artifactsWHERE file_store = 2;Or from the Rails console:
ProjectStatistics.sum(:build_artifacts_size)ProjectStatistics.sum(:pipeline_artifacts_size)The two job-artifact figures (SQL vs ProjectStatistics) should agree within a
few percent.
Interpret the ratios, not just the bytes. Compute the mean object size on both sides (bucket bytes / bucket objects, and DB bytes / DB rows). Real job artifacts average on the order of megabytes. If the bucket holds far more objects than the database with a much smaller mean size, the gap is not orphaned artifacts (an orphan has the same size distribution as a live artifact, so orphans would show matching object and byte ratios); it is versions, delete markers, or multipart parts.
Step 2: Split the bucket size by storage state
Section titled “Step 2: Split the bucket size by storage state”This is the decisive check. Get four numbers: current versions, noncurrent versions, delete markers, and incomplete multipart uploads.
- S3 Storage Lens (preferred for a yes/no answer): enable advanced metrics for the bucket and read the split from the dashboard. Takes about a day to populate.
- S3 Inventory (also gives per-prefix sizes): configure a daily report with
“all versions”,
Size, andIsLatestfields, then aggregate (for example in Athena) byis_latestand delete-marker flag. First report takes up to 48 hours.
Do not list the bucket with the AWS CLI to get sizes; it will not finish on a very large bucket.
Also check the bucket configuration:
aws s3api get-bucket-versioning --bucket <bucket>aws s3api get-bucket-lifecycle-configuration --bucket <bucket>aws s3api list-multipart-uploads --bucket <bucket> --max-items 1000- Versioning
Enabledwith no (or a long)NoncurrentVersionExpirationrule means deleted artifacts accumulate as old versions. - No
AbortIncompleteMultipartUploadrule means leftover parts accumulate. - A long
list-multipart-uploadsresponse is itself a signal; sample part sizes withaws s3api list-partson a few old uploads. - On Dedicated, check every artifacts bucket (primary, replica/Geo, and backup buckets), not only the one reported. Replication copies the accumulation.
Step 3: Check the deletion queue is draining
Section titled “Step 3: Check the deletion queue is draining”A stalled deletion backlog is a different bug from storage drift. In the Rails console:
Ci::DeletedObject.countCi::DeletedObject.where(file_store: 2).countCi::DeletedObject.group("date_trunc('day', pick_up_at)").countFailed deletions are pushed forward ~10 minutes and retried, so a backlog of
old pick_up_at dates means the drain is failing; check the
ci_deleted_objects_processing error-rate SLI (see
CI Deleted Objects Processing Triage).
Rows dated only today mean the queue is healthy.
Step 4: Scan for orphaned job artifacts (only if the gap is still unexplained)
Section titled “Step 4: Scan for orphaned job artifacts (only if the gap is still unexplained)”The orphan scans are slow and I/O-heavy. Run them only after steps 1-3 leave a byte gap attributable to current versions.
There are two scans. They cover different key layouts and each has its own delete step, so run both and never use one scan’s delete step on the other scan’s results.
| Key layout | List (read-only) | Delete |
|---|---|---|
Hashed paths outside @final/ (older layout) | untracked_object_storage_files with DRY_RUN=true | Same task with DRY_RUN=false |
@final/ paths (current layout) | list_orphan_job_artifact_final_objects | delete_orphan_job_artifact_final_objects |
Scan A: keys outside @final/
Section titled “Scan A: keys outside @final/”Dry-run scan (read-only, logs one Would move to lost and found: <key> line
per orphan):
# On a toolbox pod. It can take many hours or over a day on a large bucket, so# use nohup to keep it running after the kubectl exec session ends.nohup gitlab-rake gitlab:cleanup:untracked_object_storage_files \ BUCKETS=artifacts DRY_RUN=true > /tmp/orphan_dry_run.log 2>&1 &- The task is a dry run unless
DRY_RUN=falseis set. PassDRY_RUN=trueanyway so the command is safe to copy. - Requires GitLab 19.2+ with the batched-lookup fix (gitlab-org/gitlab!253829); before that it issued one DB query per object and cannot finish against a very large bucket.
- The toolbox pod may be recreated during maintenance; restart the scan if the output file is lost.
Scan B: keys under @final/
Section titled “Scan B: keys under @final/”nohup gitlab-rake gitlab:cleanup:list_orphan_job_artifact_final_objects \ FILENAME=/tmp/orphan_final_objects.csv > /tmp/orphan_final_list.log 2>&1 &This only writes a CSV of orphaned objects; it changes nothing in the bucket. It is also slow (about 21 hours on a multi-billion-object bucket).
Review the results
Section titled “Review the results”When the scan A dry run finishes, collect:
# Count orphansgrep -cF 'Would move to lost and found' /tmp/orphan_dry_run.log
# Sample keys to see what the orphans aregrep -F 'Would move to lost and found' /tmp/orphan_dry_run.log \ | sed -E 's/.*Would move to lost and found:? *//; s/[[:space:]].*$//' \ | shuf -n 20
# Count skipped keys and see their prefix layoutgrep -cF "doesn't match the expected format" /tmp/orphan_dry_run.loggrep -o '"file_path":"[^"]*"' /tmp/orphan_dry_run.log \ | cut -d'"' -f4 | cut -d/ -f1 | sort | uniq -c | sort -rn | head -20Skipped keys are not necessarily orphans: @final/... keys are covered by
scan B, and pipelines/<id>/artifacts/<id>/... keys are pipeline artifacts
that neither scan checks.
For scan B, the CSV has one row per orphan with its size, so count the rows and sum the size column.
Delete the orphans
Section titled “Delete the orphans”Only after the customer/SRE lead approves, and only for a scan whose results you have reviewed.
Scan A orphans (keys outside @final/). Run one of the two commands below,
not both.
Option 1: move the orphans to lost_and_found/ in the same bucket. This can be
reversed, but it frees no space.
nohup gitlab-rake gitlab:cleanup:untracked_object_storage_files \ BUCKETS=artifacts DRY_RUN=false > /tmp/orphan_move.log 2>&1 &Option 2: permanently delete the orphans instead of moving them. This cannot be undone.
nohup gitlab-rake gitlab:cleanup:untracked_object_storage_files \ BUCKETS=artifacts DRY_RUN=false DELETE=true > /tmp/orphan_delete.log 2>&1 &The real run scans the bucket again, so it acts on what is orphaned at that time, not on the lines in the dry-run log.
Scan B orphans (keys under @final/):
# Deletes exactly the objects listed in the CSV from scan B.nohup gitlab-rake gitlab:cleanup:delete_orphan_job_artifact_final_objects \ FILENAME=/tmp/orphan_final_objects.csv > /tmp/orphan_final_delete.log 2>&1 &The rollback task for this delete works on GCP only. On AWS the delete cannot be undone by GitLab.
Step 5: Decide on remediation
Section titled “Step 5: Decide on remediation”- Mostly noncurrent versions: tighten the bucket’s
NoncurrentVersionExpiration(for example 30 to 7 days). This is an infrastructure change, not a GitLab code change. Confirm first that the tenant’s backup/DR strategy does not depend on old versions (GitLab Dedicated’s documented DR uses 30-day retention; see the tenant’s Instrumentorbackups-objectstorageconfiguration). The application does not read noncurrent versions. - Mostly incomplete multipart uploads: add an
AbortIncompleteMultipartUploadlifecycle rule (for example 7 days). - Mostly orphaned current-version objects: run the delete step in Delete the orphans that matches the scan that found them.
- Healthy deletion queue but real churn: the gap is retention policy, not
drift; adjust
expire_inguidance or the lifecycle rule.