Magnetotactic bacteria (MTB) migrate in complex porous sediments where fluid flow is ubiquitous. Here, we demonstrate that magnetotaxis enables MTB to migrate effectively through porous micromodels. Directed MTB can circumvent curved obstacles by traveling along the boundaries and pass flat obstacles by repeatedly switching between forward and backward runs. Magnetotaxis enables directed motion of MTB through heterogeneous porous media, overcoming tortuous flow fields with local velocities as high as 250 μm s-1. Our findings bring new insights into the migration behaviour of MTB in their natural habitats and their potential in vivo applications as microbiorobots.