Both are extraordinary writs with deep historical roots, but they protect very different interests.
Habeas corpus and mandamus are sometimes confused because both are described as "extraordinary writs," but their purposes could hardly be more different.
What habeas corpus protects
Habeas corpus, Latin for "you shall have the body," is specifically designed to challenge unlawful detention or imprisonment. It asks a court to determine whether the government has a lawful basis for holding someone in custody, and if not, to order their release.
What mandamus protects
Mandamus, by contrast, is about compelling performance of a legal duty — it has nothing inherently to do with physical custody or detention, though the two occasionally intersect in the immigration detention context.
Where the two can overlap: immigration detention
An individual held in immigration detention while an application remains unadjudicated may, in some circumstances, pursue both remedies together — habeas corpus to challenge the legality of continued detention, and mandamus to compel a decision on the underlying application driving that detention.
Different constitutional and statutory foundations
Habeas corpus has explicit constitutional protection under the Suspension Clause of the U.S. Constitution, giving it a distinct pedigree separate from the statutory and common-law basis of modern mandamus.
Why the distinction matters for petitioners
Someone challenging the fact or duration of their detention should look to habeas corpus procedures, while someone challenging an agency's failure to make a decision should look to mandamus — and understanding which problem you actually have is the first step toward choosing the correct filing.