How to use dpkg-divert to temporarily remove/replace symlink when package is installed?

Solution 1:

If you want to divert a file owned by another package:

  • In your preinst, divert the file away.

    dpkg-divert --add --package $your_package_name --rename \
       --divert /etc/nginx/sites-available/default.disabled \
                /etc/nginx/sites-available/default
    
  • In your postrm, divert the file back.

    dpkg-divert --remove --package $your_package_name --rename \
                /etc/nginx/sites-available/default
    

This won't work for the symlink /etc/nginx/sites-enabled/default, because it's created by the nginx package's postinst rather than being part of the package. Since the symlink is part of the package, you can just rename it with mv. I don't know if this is compliant with the Debian packaging policy; if this matters to you, as this is a rather specialized question, I recommend searching or asking on debian-devel.