Skip to content

Artifact object storage size reconciliation

[[TOC]]

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.

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:

  1. 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.
  2. Incomplete multipart uploads: failed or abandoned artifact uploads leave parts behind when the bucket has no AbortIncompleteMultipartUpload rule.
  3. Orphaned job artifacts: objects under @final/ whose database row is gone. Usually a small share of the gap.
  4. Delete markers and untracked prefixes such as tmp/uploads/ and tmp/builds/.
  5. Pipeline artifacts (pipelines/<id>/artifacts/<id>/...): tracked in ci_pipeline_artifacts, not ci_job_artifacts, so a ci_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.

  • The bucket name(s) and AWS account access for S3 API calls (aws s3api).
  • Grafana/CloudWatch access for aws_s3_bucket_size_bytes_average and aws_s3_number_of_objects_average per 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_gb
FROM p_ci_job_artifacts
WHERE 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, and IsLatest fields, then aggregate (for example in Athena) by is_latest and 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:

Terminal window
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 Enabled with no (or a long) NoncurrentVersionExpiration rule means deleted artifacts accumulate as old versions.
  • No AbortIncompleteMultipartUpload rule means leftover parts accumulate.
  • A long list-multipart-uploads response is itself a signal; sample part sizes with aws s3api list-parts on 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.count
Ci::DeletedObject.where(file_store: 2).count
Ci::DeletedObject.group("date_trunc('day', pick_up_at)").count

Failed 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 layoutList (read-only)Delete
Hashed paths outside @final/ (older layout)untracked_object_storage_files with DRY_RUN=trueSame task with DRY_RUN=false
@final/ paths (current layout)list_orphan_job_artifact_final_objectsdelete_orphan_job_artifact_final_objects

Dry-run scan (read-only, logs one Would move to lost and found: <key> line per orphan):

Terminal window
# 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=false is set. Pass DRY_RUN=true anyway 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.
Terminal window
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).

When the scan A dry run finishes, collect:

Terminal window
# Count orphans
grep -cF 'Would move to lost and found' /tmp/orphan_dry_run.log
# Sample keys to see what the orphans are
grep -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 layout
grep -cF "doesn't match the expected format" /tmp/orphan_dry_run.log
grep -o '"file_path":"[^"]*"' /tmp/orphan_dry_run.log \
| cut -d'"' -f4 | cut -d/ -f1 | sort | uniq -c | sort -rn | head -20

Skipped 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.

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.

Terminal window
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.

Terminal window
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/):

Terminal window
# 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.

  • 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 Instrumentor backups-objectstorage configuration). The application does not read noncurrent versions.
  • Mostly incomplete multipart uploads: add an AbortIncompleteMultipartUpload lifecycle 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_in guidance or the lifecycle rule.