Snaps do have a purge argument but I always forget to use it. I wrote a script in Python to help with my laziness. This will only remove snap data for apps that have been uninstalled. If you want to keep the data and configs for some apps you will need to include the name in preserve list. This could be altered to handle Flatpak apps as well.
Python
x
45
45
1
#!/usr/bin/env python3
2
3
import os
4
import shutil
5
import subprocess as sp
6
7
def get_installed_snaps():
8
'''Collect installed snaps.'''
9
snap_names = []
10
result = sp.run(["snap", "list"], capture_output=True, text=True, check=True)
11
if not result.stderr:
12
raw_list = result.stdout.split('\n')
13
for app in raw_list:
14
if not app: continue
15
snap_names.append(app.split()[0].strip().lower())
16
return snap_names
17
18
def get_snap_folders():
19
'''Collect all snap app folders.'''
20
snap_confs = {}
21
home = os.path.join(os.environ['HOME'],"snap")
22
for folder in os.listdir(home):
23
if not folder.strip(): continue
24
snap_confs[folder]=os.path.join(home,folder.strip().lower())
25
return snap_confs
26
27
# User defined.
28
preserve = ["some_app_name", "another"] # Make sure the name is lowercase and matches exactly what is found.
29
do_delete = False
30
31
# Main
32
installed = get_installed_snaps()
33
configs = get_snap_folders()
34
35
for appnm,apppth in configs.items():
36
37
if appnm not in installed:
38
39
if appnm.lower() in preserve:
40
continue
41
42
print('removing ', apppth)
43
44
if do_delete:
45
shutil.rmtree(apppth)