The short story is that Castiel was working with Metatron on a spell that supposedly would lock up all the 天使 in heaven, which Cas believed would give the 天使 time to sort out their issues. He succeeds in completely the spell, but Metatron reveals that all the ingredients he gathered was for a spell that would actually cast the 天使 out of heaven. Which is what happened at the end of season 8.