Linux Commands

How to Delete a Systemd Service File

Many packages on Linux come with service files running in the background. Often, the service files are not deleted even after the associated package is deleted. Consequently, accumulating unwanted services causes extra load on the system. In such instances, it becomes imperative to delete unneeded service files.

In order to delete the service files, it is crucial to understand the set of directories that contain the service files.

Service files are typically stored in several specific directories, depending on their purpose and who installed them. A list of directories is given below.

/lib/systemd/system Service files from the downloaded packages
/etc/systemd/system Service files by the system administrator
~/.config/systemd/users Service files by normal users

So, if a package is downloaded and provides daemon and services, then these files will be stored in the /lib/systemd/system directory. The /etc/systemd/system directory contains service files created by system administrators, and only sudo users can modify them. While ~/.config/systemd/users directory contains service files created by normal users.

How to Access the Service File

The first step of deleting a service file is to find the exact path of it. To find the path, use the systemctl status command with the service name.

systemctl status [SERVICE-NAME]

To find the service name, you can list all the running services.

systemctl list-unit-files --type=service --state=running

If you want to list all the services, use the systemctl command with –type and –state options.

systemctl list-unit-files

For example, to find the unit path of myservice.service, I will execute the status command.

systemctl status myservice.service

The output shows the path of the unit file in the Loaded section.

Now that we have obtained the path of the service, we shall proceed to delete it in the subsequent step.

Warning: Before deleting the service files from the system, it is crucial to have a complete understanding of the system service files and their significance for the system. Deleting an important service file from the system may cause irreversible damage.

How to Delete the Service File

To delete the service on Linux, the systemctl and rm command line utilities will be used. Use systemctl to stop and disable the service, and then use rm to remove the service files from the respective directory.

To delete the service file, follow the command sequence given below.

sudo systemctl stop SERVICE-NAME

sudo systemctl disable SERVICE-NAME

sudo rm /lib/systemd/system/SERVICE-NAME #Service from the downloaded package

sudo rm /etc/systemd/system/SERVICE-NAME #Service by the administrator

sudo rm ~/.config/systemd/users/SERVICE-NAME #Service by the normal user

sudo systemctl daemon-reload

sudo systemctl reset-failed

Firstly, stopping the service is recommended to ensure it is not running during removal, though disabling it will also prevent it from starting again. Then it needs to be disabled, which prevents the service from starting automatically; disabling the service also removes the symbolic links created in the .wants/ or .requires/ directories. After that, remove the service files using the rm command from the respective directory.

Reload the systemd configurations using daemon-reload and the execute reset-failed command. The reset-failed command resets all the services with a failed state.

Example

In this example, let’s delete a service created by a system administrator. The service name is myservice.service and is placed in the /etc/systemd/system directory.

Check the status of the service.

systemctl status myservice.service

The service is running; note the path against the Loaded section and disable the service.

sudo systemctl disable myservice.service

It will also remove the symbolic link from the /etc/systemd/system directory.

Next, remove the service file using the rm command and service file path.

sudo rm /etc/systemd/system/myservice.service

Now, reload the systemd configuration to apply the changes.

systemctl daemon-reload

That is it! The service is removed and is no longer in your system. Verify it by checking the service status.

Conclusion

Deleting a service becomes mandatory if it is running, even if it is no longer needed. It can consume system resources if left unattended. In this guide, I covered a complete method to delete a service from Linux. First, identify the service name and path and then disable it. After that, remove the service file from the respective directory and reload the systemd configurations to complete the procedure.

About the author

Sam U

I am a professional graphics designer with over 6 years of experience. Currently doing research in virtual reality, augmented reality and mixed reality.
I hardly watch movies but love to read tech related books and articles.