Actually, they did not want to leave. Churchill was dead against it. But Churchill lost the election and Attlee came in. And even then there was only talk of dominion status. What convinced the British that they had lost India were two things -
1. Bombay Naval Mutiny - which quickly spread across India. Elements in Indian military refused to obey British officers - they flew flags of Congress and Muslim League from the ships after taking of Union Jacks
2. Red Fort Trials - To make an example of Bose's Indian National Army - A Hindu, a Muslim and a Sikh officer of the INA faced court martial proceedings in Delhi in the Red Fort. Their popularity was so high that Nehru and Jinnah both defended the INA officers and despite being convicted, the Brits had to release them.
if they really wanted to keep an imperial hold on india they could have done so using the same methods that were quite successful for them the past 2 hundred yearss, instead they let go, churchill may have been against it but he was a minority voice (there are some letters that suggest he was happy with the creation of pakistan, why do you think).
the prevailing mood, and belief was that naked imperialism is not a good method for their aims.
the british elite had envisioned what we know as globalisation decades (maybe centuries) before it occurred, imperialism does not help this so they gave the illusion of independence to their former colonies, but left in charge a brain washed and weak elite who only knew how to think, talk and act in the way of their former imperial masters told them to.
in effect they were still "imperial" subjects but took their orders in places like the UN, world bank, imf or diplomatic talks.