"then they got rid
of the foreign gods among them"
Emphasis on "then". There's a lesson here, and it's all about timing.
The Israelites ~ again ~ did evil in the eyes of the Lord. They served the Baals, and the Ashtoreths and gods of Aram and Sidon and Moab and Ammon and the Philistines... man, there's just no end to the number of gods these Israelites would serve, is there?
So the Lord gave them over to the Philistines. And the Israelites were in distress, so they cried out to the Lord, "We have sinned against you, forsaking you and serving the Baals!"
God's response is surprising. Unusual. He refused. "I have saved you repeatedly. But you have forsaken Me. Go and cry out to the gods you have chosen. Let them save you!"
The Israelites begged. "We have sinned. Do with us whatever you think best, but please rescue us now!"
And then, they got rid of the foreign gods among them and served the Lord.
And He saw the sincerity He was looking for, and rescued them.
They saw their error, and yet they asked for God's help without doing anything to rectify the problem?? I'd really like to mock them mercilessly, except I'm pretty sure I'm guilty of the same thing.
Note to self: Confess and repent of my sins. It will make me more credible when I ask God for His help.
~ "Repent, then, and turn to God,
so that your sins may be wiped out,
that times of refreshing may come
from the Lord" ~