"Do not cast me from your presence or take your Holy spirit from me..." David cried desperately to God because he knew very well how terrible life without God would be. He had no excuse with which to justify himself, when the prophet Nathan came to rebuke him because of the sin he committed against God (2Samuel 12: 1 - 10). the Lord said to him "you struck down Uriah the Hittite with the sword and took his wife to be your own ..Now therefore, the sword shall never depart from your house, because you despised me and took the wife of Uriah the Hittite to be your own". David knew that the sin that he had committed was enough reason for God to cast him out of his presence, and to take his Spirit from him.
At that moment, David will surely have been remembering when God cast Saul, his predecessor, out of His presence, and took His Spirit from him. He would also have remembered that God did this because of Saul's disobedience. Seeing the prophet Nathan approaching him, he would probably have vividly pictured the Prophet Samuel approaching King Saul to rebuke him for his sin. Saul then, filled with anger and jealousy toward David, tried on many occasions to kill him. Bearing all these things in mind David cried out to God asking forgiveness "do not cast me out of your presence or take your Holy Spirit from me".
David might also have remembered what had happened to King Nebuchadnezzar and to Samson the Judge. Due to his pride, God had driven King Nebuchadnezzar away from his people and made him eat grass like the cattle. His body was drenched with the dew of heaven until his hair grew like the feathers of an eagle and his nails like the claws of a bird (Daniel 4: 33). Samson, a mighty ruler who struck down a thousand Phillistine men with a jaw-bone of a donkey, was left by God and was used by his enemies to entertain the crowd due to his folly (and even to reveal the secret of the power which had been given to him by God to a woman) (Judges 16: 20 - 21).
Even today, God is taking His Spirit from His people due to reasons which he makes known to them. They might not be committing the same sins as David, Nebuchadnezzar or Samson. Have you ever come across people who were once used powerfully by the Spirit to preach the Gospel, and even heal the sick, but now they are as dry as any unbeliever and Godless? If we are not careful it can happen to us too.
If there's anything in your life that you suspect may cause God to cast you out of His presence and take His Spirit from you, then do not delay but pray as David did "Do not cast me out of your presence or take your Holy Spirit from me". the Lord is gracious and he will forgive your sins if you are truly repentant.