That sludge is really just a mix of soap, water, skin cells, hair, tooth paste and all the other lovely stuff that goes down your drain. I doubt it is fungus.
You can't really prevent it. There are all sorts of chemical and non-chemical mixtures you can try to clean it out, but I think removing the trap and cleaning it out is the best bet. If the trap is easy to remove, why pour harmful chemicals down and into your water supply?
If the sludge is soft and comes off easily, you might have some luck filling the sink with really hot water and then draining it quickly.
I'm coming in a little late to help the OP, but wanted to add some clarification in case anyone else runs into this situation. I used the procedure outlined below dozens of times when I was a repair plumber in the 80's.
The trap arm going to the wall is 1 1/2" OD tubular drawn brass (usually chrome plated). It's going into a cast iron "sanitary tee" in the wall, with an inlet that has 1 1/2" female IPS (iron pipe size) threads. The original connection was made with a so-called Marvel Adapter, which is a brass ring with 1 1/2" IPS male threads on the outside, and a smooth 1 1/2 ID inner surface that was soldered to the tubular brass trap arm. It does tend to look like some kind of leaded joint because all that's visible is the edge of the ring/adapter and the excess solder from when it was sweated on.
To remove it, first break out the old trap arm (as the OP said he accidentally did) - often a few hammer blows is all it takes, but sometimes you need to cut it off flush with a Sawzall or hacksaw. The OP's 3rd picture above shows it in this state.
Next, insert your Sawzall blade (with fine metal-cutting teeth) or the end of a hacksaw blade into the opening, preferably near the top, and make a small cut going outward. You'll be cutting through the relatively soft brass, but you should stop cutting when the sound and feel changes once the blade starts to hit the cast iron threads. Now move your saw blade over about 1/2" and make a second cut just like the first one. Then use a flat-bladed screwdriver or small chisel and tap out the 1/2" section of the brass ring you just cut. Then use the same tool to drive the rest of the ring loose - drive the screwdriver tip under one end of the remaining ring until it bends inward sufficiently far that you can grip it with a pair of pliers. Then you should be able to easily un-thread it the rest of the way out.
Now you'll have some nicely intact threads in the cast iron tee you can use. I like to use an old 1 1/2" galvanized nipple with a few axial grooves cut through the threads as a "thread-chaser" to clean out as much old rust and gunk out as I can - the first 3 or 4 threads that were in contact with the brass ring should be fine but it's a good idea to try to clean up the deeper threads too.
Now you should be able to thread an ABS or PVC male adapter into the cast iron threads (using some Rectorseal #5 thread sealant or equivalent). If you can't get a good "bite" with the plastic male adapter, you can thread a short galvanized nipple in there instead, then thread a plastic female adapter onto that. Glue in whatever length of plastic (ABS/PVC) pipe you need, and then a regular compression-ring trap adapter or whatever else you need to connect your trap.
Here's what that Marvel Adapter looks like new. These were real common 50+ years ago:
https://www.ferguson.com/product/sioux-chief-marvel-brass-marvel-thread-adapter-ring-s274/_/A-ProdFamily-216152
Best Answer
That style of sink drain that you show has a trap at the bottom. A trap on a sink, regardless of the style you show or the traditional U style, serves two purposes:
This means that any water device that has a drain in a house must have some trap. IF your sink did not have the one shown in the picture, chances would have been that it is down stream somewhere. In both styles, you simply can unscrew where it attaches with a wrench of appropriate size and the trap comes off.
Note: Keep in mind that after retrieving your item to let water flow through it for a little bit again as this puts a layer of water back in the trap which is what really protects from sewer gasses.