How to add uninstall script to debian package

2 min read 06-10-2024
How to add uninstall script to debian package


Adding an Uninstall Script to Your Debian Package: A Comprehensive Guide

Creating a Debian package is a powerful way to distribute your software. However, ensuring a clean and reliable uninstallation process is equally crucial. This guide will walk you through adding an uninstall script to your Debian package, enabling users to remove your application smoothly.

The Problem:

Imagine you've developed a fantastic application and packaged it for Debian users. However, when a user wants to remove it, they are left with orphaned files and configuration settings, creating a messy system. An uninstall script ensures a clean and predictable removal of your application.

Scenario & Original Code:

Let's assume you've created a simple "my-app" application. Here's a basic control file for your Debian package:

Package: my-app
Version: 1.0.0
Architecture: amd64
Maintainer: Your Name <[email protected]>
Description: A simple application
Depends: libc6

The Solution:

To add an uninstall script, we'll modify the control file and create a postrm file.

  1. Modify the control file:

    Package: my-app
    Version: 1.0.0
    Architecture: amd64
    Maintainer: Your Name <[email protected]>
    Description: A simple application
    Depends: libc6
    Pre-Depends: debian-helper
    # Add the following line:
    Post-rm: /path/to/postrm.sh
    
  2. Create the postrm script:

    Create a file named postrm.sh in your package directory. This script will be executed during the uninstallation process.

    #!/bin/bash
    set -e
    
    if [ "$1" = "remove" ]; then
        # Remove the application's configuration files:
        rm -rf /etc/my-app 
        # Remove the application's data directory:
        rm -rf /var/lib/my-app
        # Remove any other relevant files or directories
    fi
    

Explanation & Best Practices:

  • Pre-Depends: debian-helper: This ensures the package relies on the 'debian-helper' utility which provides essential functions for post-installation and removal scripts.
  • postrm.sh: The uninstall script is named postrm.sh and placed in the root directory of your package. It is executed during the uninstallation process.
  • Conditional logic (if [ "$1" = "remove" ]; then ... fi): This ensures the script only executes during package removal.
  • set -e: This ensures the script exits immediately if any command fails.

Additional Tips:

  • Use dpkg-buildpackage -b to rebuild your package after making changes.
  • Consider using dpkg-deb -x to extract the contents of your package and test the postrm script before packaging.
  • Thoroughly test your uninstall script to avoid leaving behind unwanted files or configurations.

Example:

Suppose you want to remove a log file created by your application during the uninstall process. Add the following line to your postrm.sh:

rm /var/log/my-app.log

Conclusion:

By adding an uninstall script to your Debian package, you can provide a seamless and reliable uninstallation experience for your users. Remember to test your script thoroughly and follow best practices to ensure a clean and predictable removal of your application. This step is essential for a user-friendly and professional package.