Remove dangling symlinks

In the docs, post upgrade tasks there is mentioned to remove dangling symlinks.
Is it save to recursively remove dangling symlinks from /usr.
The documentation states to review the list of broken symlinks first.
How can I audit this list correctly?

2 Likes
2 Likes

Thanks Vladislav
However what triggered me to ask the question here was this sentence in the article: ‘Be extremely wary of running these commands as root, or on system directories.
How do I verify it is safe to remove them all at once.

2 Likes

Yes, before deleting anything you should verify it is safe to proceed.
However, I’m afraid there’s no simple method, as the issue in general is not limited to symlinks.

You can safely remove only leftover files, symlinks and empty directories not owned and not dynamically created by any installed package.

Package ownership can be tested like this:

rpm -q -f /path/to/file

You must not remove files and symlinks owned by any installed package and non-empty directories, since files within them can be part of some package.

For example, I can safely remove the following empty directories:

> find /usr/share -type d -empty -exec rpm -q -f {} + \
| grep -e " is not owned by any package$"
file /usr/share/torbrowser-launcher is not owned by any package
file /usr/share/syslinux is not owned by any package
file /usr/share/virt-manager/ui is not owned by any package

However, most difficult to identify files and directories which are dynamically generated by installation, startup and setup scripts.

I must not remove the following files as I remember those are procedurally generated as part of the system setup:

> find /usr/share -type f -exec rpm -q -f {} + \
| grep -e " is not owned by any package$"
file /usr/share/glib-2.0/schemas/gschemas.compiled is not owned by any package
file /usr/share/mime/packages/custom.xml is not owned by any package
file /usr/share/mime/application/x-vnc.xml is not owned by any package
file /usr/share/mime/application/pkix-crl+pem.xml is not owned by any package
file /usr/share/mime/application/x-netinstobserver.xml is not owned by any package
file /usr/share/mime/application/x-iptrace.xml is not owned by any package
...
file /usr/share/applications/mimeinfo.cache is not owned by any package

I must not remove the following broken symlinks since they are owned by a package and exist for a reason:

> find /usr/lib -xtype l
/usr/lib/modules/6.1.6-200.fc37.x86_64/source
/usr/lib/modules/6.1.8-200.fc37.x86_64/build
...

> rpm -qf /usr/lib/modules/6.1.6-200.fc37.x86_64/{source,build}
kernel-core-6.1.6-200.fc37.x86_64
kernel-core-6.1.6-200.fc37.x86_64

In short, I recommend to refrain from low-level destructive actions, unless you have a specific problem and fully understand what you are doing and how to revert the changes.

1 Like

It is not safe. Symlinks are often used to record information in the filename, and don’t actually point to a file, so are dangling but not safe to remove. In a list of dangling symlinks you may recognize some you created in the past and forgot to remove, so those are certainly safe to remove, but symlinks you didn’t created need to be investigated before removal.

2 Likes