This question was asked almost 7 years ago now, but I stumbled across it in a Google search for a similar problem and thought I'd post an answer anyway, since the question has an answer that isn't really a solution.
The following question is also related (but pertains to /usr/local instead of /opt) and can be solved similarly:
dpkg: warning: while removing directory /usr/local not empty so not removed
I was able to solve this problem using the following technique.
A Debian package (.deb) is just an "ar" archive file with 3 members:
$ ar t package.deb
debian-binary
control.tar.gz
data.tar.xz
The data.tar.xz member may be named data.tar.gz depending on the age of the *.deb. Adjust commands accordingly.
The files that get installed to the target system are contained in the data.tar.xz member. If you extract the data.tar.xz member and list its contents, you will see something like the following for example.
$ ar p package.deb data.tar.xz | unzx -c | tar t
./
./opt/
./opt/myCompany/
./opt/myCompany/myProgram
Removing the ./opt/ directory member (but not its contents) from the data.tar.xz file will stop dpkg from trying to remove the /opt directory when the package is uninstalled.
$ ar x package.deb data.tar.xz
$ unzx data.tar.xz
$ tar --delete --occurrence -f data.tar ./opt/
$ #tar --delete --occurrence -f data.tar ./usr/local/
Now, if you list the contents of the data.tar file, you should see something like:
$ tar tf data.tar
./
./opt/myCompany/
./opt/myCompany/myProgram
- Notice the ./opt/ member has been removed.
The last step is to re-compress the data.tar file and replace the data.tar.xz member in the deb with the modified one.
Here is one example of the complete process of removing the ./opt/ entry from the tar file archive and replacing the tar file archive in the deb with the modified one:
$ ar x package.deb data.tar.xz
$ unzx data.tar.xz
$ tar --delete --occurrence -f data.tar ./opt/
$ xz data.tar
$ ar r package.deb data.tar.xz
$ rm data.tar.xz
Now, dpkg will not try to remove the /opt directory when the deb is removed/uninstalled from the system.