When you need this
- The OCIR storage line on the cost report keeps growing every month.
- CI pushes an image per commit and nobody has cleaned up in a year.
- A repository has hundreds of versions and the console list is unusable.
- You want automatic deletion, but you can't risk removing an image a production cluster still pulls.
This cheat sheet covers the CLI commands for finding and removing old images, and how the built-in retention policies work. Retention policies are configured in the console. The page linked from the Container Registry docs does not describe CLI commands for them.
Warning
A deleted image can be restored for up to 48 hours only. After that it is gone. Before you delete anything in bulk, check which images running workloads still use (see "Check what is in use").
Set up variables
export C=ocid1.compartment.oc1..aaaa... # compartment that holds the repositories
export REGION=eu-frankfurt-1 # registries and retention policies are per region
export OCI_CLI_REGION=$REGION
Repositories live in a region. If you push to several regions, repeat every step below in each one.
Find the repositories that use the most storage
oci artifacts container repository list -c $C --compartment-id-in-subtree true --all \
--query 'data.items[].{name:"display-name", images:"image-count", gb:"billable-size-in-gbs", public:"is-public", immutable:"is-immutable"}' \
--output table
Sort by size on the client side:
oci artifacts container repository list -c $C --compartment-id-in-subtree true --all \
--query 'sort_by(data.items, &"billable-size-in-gbs")[-10:].{name:"display-name", gb:"billable-size-in-gbs", images:"image-count"}' \
--output table
Note
Output field names such as
billable-size-in-gbsandimage-countcan vary by CLI version. If a column comes back empty, run the command with--output jsonand check the real names.
List images in a repository
| Goal | Command |
|---|---|
| All images, newest first | oci artifacts container image list -c $C --repository-name app/api --sort-by TIMECREATED --sort-order DESC --all |
| Images with no tag (untagged digests) | oci artifacts container image list -c $C --repository-name app/api --is-versioned false --all |
| Images with at least one tag | ... --is-versioned true |
| A specific tag | ... --image-version 1.42.0 |
| A specific digest | ... --image-digest sha256:... |
| Only available (not deleted) images | ... --lifecycle-state AVAILABLE |
| Details for one image, including last pull time | oci artifacts container image get --image-id <image-ocid> |
A compact view:
oci artifacts container image list -c $C --repository-name app/api --all \
--sort-by TIMECREATED --sort-order ASC \
--query 'data.items[].{id:id, tag:version, digest:digest, created:"time-created"}' \
--output table
image get returns the full list of tags on the image and a last-pulled timestamp (time-last-pulled in current versions). That timestamp is what you want when deciding what is really stale.
Check what is in use
Before deleting, collect the images your clusters actually run.
On OKE (or any Kubernetes cluster pulling from OCIR):
kubectl get pods -A -o jsonpath='{range .items[*]}{range .spec.containers[*]}{.image}{"\n"}{end}{range .spec.initContainers[*]}{.image}{"\n"}{end}{end}' \
| grep ocir.io | sort -u > in-use.txt
Also check:
- Deployment and job manifests in Git, including rollback targets one or two releases back.
- Container Instances and Functions that reference OCIR images (
oci container-instances container-instance list,oci fn function list). - Helm values that pin a tag that isn't currently deployed but is your documented rollback.
Caution
Functions and Container Instances pull the image again on cold start or restart. Deleting the image they reference doesn't break them immediately; it breaks them at the next restart, often at night.
Delete, untag and restore
| Task | Command |
|---|---|
| Delete an image (all its tags) | oci artifacts container image delete --image-id <image-ocid> --force |
| Remove one tag but keep the image | oci artifacts container image remove-version --image-id <image-ocid> --image-version 1.41.0 |
| Restore a deleted image (within 48 hours) | oci artifacts container image restore --image-id <image-ocid> --image-version 1.41.0 --wait-for-state AVAILABLE |
| See deleted images still restorable | oci artifacts container image list -c $C --repository-name app/api --lifecycle-state DELETED --all |
The docs say a deletion can take up to 48 hours to take effect and release the storage, so the cost report won't drop the next day.
Bulk delete untagged images older than 30 days
Review the list first, then delete:
CUTOFF=$(date -u -d '30 days ago' +%Y-%m-%dT%H:%M:%S)
oci artifacts container image list -c $C --repository-name app/api \
--is-versioned false --lifecycle-state AVAILABLE --all \
--query "data.items[?\"time-created\" < '$CUTOFF'].id" --raw-output \
| jq -r '.[]' > to-delete.txt
wc -l to-delete.txt # review the count and spot-check a few IDs
while read -r id; do
oci artifacts container image delete --image-id "$id" --force
done < to-delete.txt
Keep to-delete.txt. If someone reports a broken pull in the next two days, the OCIDs are all you need to restore.
Keep the last N tagged images
KEEP=20
oci artifacts container image list -c $C --repository-name app/api \
--is-versioned true --lifecycle-state AVAILABLE --all \
--sort-by TIMECREATED --sort-order DESC \
--query 'data.items[].{id:id, tag:version}' --output json \
| jq -r ".[$KEEP:][] | \"\(.id) \(.tag)\"" > candidates.txt
Remove anything in in-use.txt from candidates.txt before deleting.
Retention policies
Retention policies delete images automatically. The rules, from the Container Registry docs:
| Item | Behaviour |
|---|---|
| Scope | Per region. Each region has one global policy, plus optional custom policies |
| Default global policy | Retains all images; nothing is deleted until you change it |
| Custom policy | Overrides the global policy for the repositories you add to it |
| Repositories per policy | A repository can belong to only one custom policy at a time |
| Criteria | Images not pulled for N days; images not versioned (tagged) for N days |
| Exempt versions | Comma-separated tags with * wildcards, for example latest,prod-*,*-rc. Applies to the "not pulled" criterion only |
| Schedule | An hourly process checks images; new or edited policies have a cooling-off period of several hours |
| Permissions | manage on the tenancy to create or edit policies; manage on a repository to add it to a policy |
Console path: Developer Services → Container Registry → (region) → Image retention policies. Edit the global policy, or add a custom ("override") policy and attach repositories.
A practical starting point:
- Global policy: delete images not versioned for 30 days (cleans up untagged digests from CI rebuilds).
- Custom policy for CI repositories: delete images not pulled for 90 days, exempting
latest,prod-*,release-*. - No policy (or a very long one) for base images and anything used by DR environments that only pull during a failover.
Warning
"Not pulled for 90 days" will delete the image a DR cluster or a quarterly batch job needs, because neither pulls it regularly. Put those repositories in a custom policy with a long window, or exempt their tags.
Policies must be set up identically in every region you replicate images to. They do not copy across regions.
Prevent it next time
- Push with immutable, meaningful tags (
1.42.0,git-<sha>) and promote by tag, so retention exemptions likeprod-*actually protect something. - Set the global retention policy in every region the day you create the first repository.
- Track repository size monthly with the
repository listquery above, or from the cost report filtered to Container Registry. - Keep CI from pushing on every branch build; push only from main and release branches.
- Write the restore command into the runbook next to the delete script. The 48-hour window is short over a weekend.
Get the weekly HubDatAI email
One email a week with everything new: posts, learning sessions and videos. Free, no spam, unsubscribe in one click. What you get