Local image cache cleanup

When I create an instance from an image repository, image is pulled and stored locally as a zfs dataset (or 2 datasets in case of VMs).

After deleting instance, images stick around forever and add up in used storage space, especially the vm.block despite no instance using them.

lxc image list doesn’t show whether they are used by an instance.

How can I determine zombie images and clear them periodically, without the hard way of zfs list and reverse comparing with instances base image lxc ls --all-projects -c F

zfs list -o name,origin should provide you a good starting point. Also, you can check which image was used for a given instance with: lxc config get $INSTANCE volatile.base_image.

Doea “lxc image show (hash)” show what’s using it?

It doesn’t.
Also they generally show Last used: never despite being at least once used.

lxc image show 331b80c55fa4
auto_update: true
properties:
  architecture: amd64
  description: Ubuntu jammy amd64 (20230331_07:42)
  os: Ubuntu
  release: jammy
  serial: "20230331_07:42"
  type: disk-kvm.img
  variant: default
public: false
expires_at: 1970-01-01T00:00:00Z
profiles:
- default
lxc image info 331b80c55fa4
Fingerprint: 331b80c55fa4fefffad21eca8c7e1eb12e467dfc119d4dddd44b30f98f00e90e
Size: 264.28MB
Architecture: x86_64
Type: virtual-machine
Public: no
Timestamps:
    Created: 2023/03/31 00:00 UTC
    Uploaded: 2023/04/01 09:31 UTC
    Expires: never
    Last used: never
Properties:
    release: jammy
    serial: 20230331_07:42
    type: disk-kvm.img
    variant: default
    architecture: amd64
    description: Ubuntu jammy amd64 (20230331_07:42)
    os: Ubuntu
Aliases:
Cached: no
Auto update: enabled
Source:
    Server: https://images.linuxcontainers.org
    Protocol: simplestreams
    Alias: ubuntu/jammy
Profiles:
    - default

lxc ls --all-projects -c f
+--------------+
|  BASE IMAGE  |
+--------------+
| 0eaddc4543be |
+--------------+
| 0eaddc4543be |
+--------------+
| 5d3375d741f4 |
+--------------+
| 8f07868ec7b8 |
+--------------+
| 9efd73850b1e |
+--------------+
| 331b80c55fa4 |
...