This is not often said, but most of the magnetization of a submarine is produced by the friction of water and air on the ship (MagnetoHydroDynamics).
Some U-Boots had an MES system (Magnetische EigenSchutzanlage), means "on-board magnetic protection system", which is therefore a "degaussing" technique.
Some small cargo ships have been modified into "Sperrversuchsschiff" (experimental anti-mine ship) including the DORPAT or the MEERSBURG, ex-KONSTANZ. These experiments gave rise to the "Sperrbrecherflottillen". The "Sperrbrecher" (blockade by mines breaker) had an MES and were often used when entering or leaving a port of a U-Boot from its U-Bunker to create a safe passage for it.