In addition to other answers, I found a number of snap files of old versions that did not appear in the output of the snap list -all
command.
Those were mainly chromium
and firefox
.
So here is a perhaps over-engineered python script to remove snap files that do not correlate to any of the version listed in snap list -all
(use with caution).
#!/usr/bin/env python3
import subprocess
from pathlib import Path
process = subprocess.run('snap list --all', shell=True, check=True, stdout=subprocess.PIPE)
text_lines = process.stdout.decode('utf-8').splitlines()
column_values_by_line = [
[value for value in text_line.split(' ') if value]
for text_line in text_lines
]
header_names = column_values_by_line[0]
snap_list = [
dict(zip(header_names, column_values))
for column_values in column_values_by_line[1:]
]
expected_snap_filenames = sorted([
f'{snap["Name"]}_{snap["Rev"]}.snap'
for snap in snap_list
])
print('expected snap files:', expected_snap_filenames)
snap_root_dir = '/var/lib/snapd/snaps'
existing_snap_filenames = sorted([
path.name
for path in Path(snap_root_dir).glob('*.snap')
])
print('actual snap files:', existing_snap_filenames)
obsolete_snap_filenames = sorted(set(existing_snap_filenames) - set(expected_snap_filenames))
print('obsolete snap files:', obsolete_snap_filenames)
if len(obsolete_snap_filenames) == len(existing_snap_filenames):
raise AssertionError('something seems wrong, should not remove all of the snap files')
if obsolete_snap_filenames:
remove_command = ['sudo', 'rm', '-f'] + [
Path(snap_root_dir).joinpath(obsolete_snap_filename).as_posix()
for obsolete_snap_filename in obsolete_snap_filenames
]
print('running:', ' '.join(remove_command))
user_input = input('Confirm? [Y/N] ')
if not user_input.lower() in ('y', 'yes'):
print('aborting')
else:
subprocess.run(remove_command, shell=False, check=True)
print('done')
else:
print('no obsolete snap files found')