Updated: Nov 20, 2021
David knows he is the worst of sinners just as we know we are the worst of sinners. So David also knows that according to God's steadfast love he will be forgiven. The consequences of his sin are still before him. You see he is the God of abundant mercy as we have seen through Christ.
Create in Me a Clean Heart, O God
To the choirmaster. A Psalm of David, when Nathan the prophet went to him, after he had gone in to Bathsheba.
51 Have mercy on me,[a] O God, according to your steadfast love; according to your abundant mercy blot out my transgressions. 2 Wash me thoroughly from my iniquity, and cleanse me from my sin!
3 For I know my transgressions, and my sin is ever before me. 4 Against you, you only, have I sinned and done what is evil in your sight, so that you may be justified in your words and blameless in your judgment. 5 Behold, I was brought forth in iniquity, and in sin did my mother conceive me. 6 Behold, you delight in truth in the inward being, and you teach me wisdom in the secret heart.
7 Purge me with hyssop, and I shall be clean; wash me, and I shall be whiter than snow. 8 Let me hear joy and gladness; let the bones that you have broken rejoice. 9 Hide your face from my sins, and blot out all my iniquities. 10 Create in me a clean heart, O God, and renew a right[b] spirit within me. 11 Cast me not away from your presence, and take not your Holy Spirit from me. 12 Restore to me the joy of your salvation, and uphold me with a willing spirit.
13 Then I will teach transgressors your ways, and sinners will return to you. 14 Deliver me from bloodguiltiness, O God, O God of my salvation, and my tongue will sing aloud of your righteousness. 15 O Lord, open my lips, and my mouth will declare your praise. 16 For you will not delight in sacrifice, or I would give it; you will not be pleased with a burnt offering. 17 The sacrifices of God are a broken spirit; a broken and contrite heart, O God, you will not despise.
18 Do good to Zion in your good pleasure; build up the walls of Jerusalem; 19 then will you delight in right sacrifices, in burnt offerings and whole burnt offerings; then bulls will be offered on your altar.