In the fourth Codex Alera book Captain's Fury, the First Lord Gaius Sextus has one of these moments when he deliberately triggers a volcano over the city of Kalare, wiping out everyone in the city. He's forced to do this because High Lord Kalarus was planning to wait until hundreds of thousands of people, refugees and Legion troops from both his army and the loyalist Alerans had entered the city before triggering the volcano to kill everyone. Thanks to Gaius, only the city was wiped out, instead of the region. It did destroy all of the smaller towns and steadholts in within the ring of mountains surrounding Kalus, but it was still far less than would have happened if Kalarus had had his way.
Inverted in Lois McMaster Bujold's Barrayar, newly-made Regent Aral Vorkosigan is faced with either upholding the rule of law, or sparing the life of a boy who accidentally killed someone in a duel—a crime punishable by death. He eventually chooses the law, but feels miserable about it. Even knowing that he Did What He...you get the idea...doesn't comfort him much.
Subverted in Terry Pratchett's Night Watch, where Vetinari suggests that a monument be erected to the watchmen who died in the Glorious Revolution of May 25th, engraved with the phrase "They Did the Job They Had to Do." Vimes angrily replies, "No! They did the job they didn't have to do, and they died doing it, and you can't give them anything."
Colonel-Commissar Gaunt leaving Tanith to burn at the beginning of Warhammer 40,000: Gaunt's Ghosts. Tanith could not have been saved no matter what he did. It is the act of not letting the Tanith soldiers stay behind and die for their planet that invokes this trope. He made them 'ghosts' because that is what he had to do as a loyal officer of the Imperium.
Raj Whitehall's computer mentor, a Well-Intentioned Extremist if ever there was one, and his wife Suzette are always telling him this one in The General. To Raj's credit he never quite accepts it.
In Red Harvest by Dashiell Hammett, "a man's got to do what a man's got to do" is favorite phrase of Noonan the chief of police. Ironically he uses it any time he does something that would benefit him — such as manipulating a witness to convince her that her husband's killer was the man he had a grudge against. The Continental Op repeats the phrase before searching a dead lawyer's pockets for potential blackmail materials.
World War Z had the Redeker Plan. Which is basically a strategy for using refugees as zombie bait while government forces regroup. Despite the horrific inhumanity of the plan, the world's governments get so desperate that they use it anyway... and the plan arguably saves humankind (or, at the very least, prevents said governments from collapsing and leaving the world in anarchy).
In the HaloExpanded Universe novel Ghosts of Onyx the UNSC used and sacrificed HUNDREDS of little orphaned kids, refugees from planets devastated by the Covenant, as Laser guidedTyke Bombs. If it wasn't for this strategy the humans would have lost the war before Halo 3 rolled around.
This is also Dr. Catherine Halsey's excuse for creating the SPARTAN-IIs. This was actually considered worse than creation of SPARTAN-IIIs (the aforementioned orphans). 75 children were kidnapped from their families and replaced with flash clones (which swiftly die) and then goes about turning the children she'd taken into nothing more than killing machines. Do to this, she has the SPARTANS undergo experimental and highly-dangerous medical treatments that kills 30 and permanently disfigures 12. It should be noted that Halsey's guilt about this was quite considerable... to the point where she goes to the opposite extreme. In later books, she's willing to see Earth and most of humanity annihilated if it saves her SPARTANs. In the end it was justifiable, as one of them manages to end the war and saves the entire galaxy from either assimilation by the Flood or death via the Halo Array.
In Summer Knight, Aurora says that she must stop the interchange between the Summer and Winter Courts, and it's horrible but she didn't set the price.
In Changes this is what Harry does. He'll go all out to do anything for his goal to save his daughter's life, including taking the position of the Winter Knight, killing the old one in the process, getting all his friends at risk of death, even destabilizing the White Council and most of all, killing his lover to use the very dark magic his enemies sought to use on him right back at them.
Harry:I used the knife. I saved a child. I won a war. God forgive me.
In Ben Counter's Warhammer 40,000 novel Chapter War, the Inquisitor Thaddeus lied to get aboard a Howling Griffons' ship. When they came to a head, and they try to imprison him, he kills one to escape. He once "would have never accepted the death of a good Imperial servant as a necessary evil. But he was much wiser now."
This is a major theme in the Wheel of Time with the three male main characters forced to act progressively more brutal as their responsibility grows and the situation worsens. Includes such gems as using friends like pawns, executing friends, torture by way of dismemberment, allowing bad guys to enslave hundreds of women in order to secure a temporary military alliance, creating an army of superpowered men who will eventually go insane, and purposely destroying your own humanity in order to be up for the job of saving the world.
Inverted in that Rand's inability to kill any woman, even when she's a villain, causes his Obi Wan to die.
The witch Senna Wales of Everworld, as mentioned above, occasionally says something to this effect to the other main characters in reference to her schemes, which often involve some poor sap being bewitched.
Brent Weeks' The Night Angel Trilogy is full of these, usually from Logan. When newly made King Logan flips a table, breaks a leg off and brutally smashes in the arms and legs of his best friend Kylar, who is slowly dying a torturous death on 'the wheel'. He then gives an ultimatum to the Laeknaught,shattering his "good boy" image. He also says this verbatim to Count Drake after he bends knee in allegiance to Terah Graesin.
In Hero by Perry Moore, Goran uses this exact phrase when Thom asks him how he got himself and his little brother out of their war-torn homeland. What makes it chilling is that that's all he says; we never find out what he actually did.
This is a running theme in David Drake's Hammers Slammers stories: in war, you can't keep your hands clean unless you want to lose. The Slammers are well aware of this. Some of the groups that hire them are shocked to find it out — the hard way.
Mr. Sellars in Otherland, the Mysterious Informant for the good guys, is very much The Chessmaster and shamelessly manipulates people in order to advance his schemes, the most disturbing of whom is an innocent six-year old girl. In the end, his justifications ring hollow even to himself, especially once his dark secret is revealed and it turns out that his core motivation was entirely selfish.
Livia in I, Claudius ruthlessly manipulates and kills family members and anyone else close to them to ensure her son becomes emperor and Rome does not return to being a Republic, convinced this is the only way for the city to remain great.
Both Dumbledore and Snape from Harry Potter. Also implied when describing Crouch's decision to allow aurors to use the Unforgivable Curses during the first war with You-Know-Who. And Harry himself doesn't come away all squeaky clean in The Deathly Hallows either, throwing around Imperius-curses left and right. Perhaps his worst example of this trope is when he goes Crucio on the Carrows' arses (probably his first Unforgivable ever), to say nothing of how even the protagonists throw Unforgivables every which way but loose and any which way they can during their war against Voldemort.
When George shoots Lennie in Of Mice and Men. After Lennie kills Curley's wife, George is forced to shoot him so the other men don't. He does it in a way so Lennie doesn't realise, which the other men would not have given the courtesy of doing.
In Death: Eve and Roarke have had to defend their actions more than once and they have even said this trope practically word for word to each other.
Discourses on Livy talks about how necessity must sometimes trump what is good for the sake of preserving liberty.
In Poul Anderson's "The Burning Bridge", when a message from Earth is causing people to want to turn back, The Captain fakes a more imperious message to inspire them to go on — and when he's caught, makes it appear that the crewmember had gone crazy and has them put him into deep sleep.
In the Indian novel The White Tiger: Balram believed that the only way he could have become an entrepreneur and broken out of the coop was to kill Ashok. Hell, when you look at how things work, he's probably right.
The rulers of Kegan in a book of Jedi Apprentice made their planet into a Stepford-Smiling mess, but feel justified in not just doing it, but preserving the status quo.
Oddly they do seem to have actually made correct predictions about the rise of The Empire. However even though they were correct, it wouldn't have done them any good as even isolated there was nothing to stop the Empire from coming after them.
Trapped on Draconica: Taurok hates every single order Gothon gives him over the course of the story but follows them to keep his family safe.]]
In The Foundation, one of Salvor Hardin's mottos is "Never let your morals prevent you from acting correctly".
Adventure Hunters: Both Ryvas and Marcus are fully aware of the villness of their actions but believe them to be necessary to save the kingdom.
A common trope invoked in so-called "men's adventure" literature of the early 1970s that featured heroes (usually spies, or soldiers) committing de facto murder in order to finish their missions, such as Mack Bolan (The Executioner), Death Merchant, and COBRA. In at least two COBRA novels, the "hero" (Jon Skul) kills police officers and innocent bystanders in order to complete his missions. When called to task for the former, he more or less invokes the trope while actually criticizing the person for being upset about cops being murdered.
A common theme in "Literature/ANIMORPHS". The main characters (who are only 13-16, mind you) are forced to do some terrible things to prevent both humans and countless other alien species being enslaved by a Puppeteer Parasite species. This includes trapping another kid in a rat's body because he turned traitor, killing sentient beings so their bodies can't be used as hosts, manipulating an Actual Pacifist to go against their nature to kill...in the very last book they kill tens of thousands of unprotected yeerks. When they're put on trial for war crimes, this is their justification.
Specifically, deliberately, and explicitly Inverted by the Knights Radiant of The Stormlight Archive. The first Oath every Radiant swears is "Journey before destination", which can be paraphrased as "The actions you take are the reality, not the goals that you aim to achieve through those actions. No goal can justify an immoral act."