For this, I envision a system in which a special functional voxel is used to join two structures together when powered. If the power fails, e.g. in the event of a ship attack, or is switched off manually, the structures detach. This could be used to make the scenario you're proposing.
For real though, if you're being attacked by pirates and you detach your cargo bays, you're serving them loot on a platter. You need a defense system capable of dealing with threats like that.