You could loop through the character, and check if it is a hat, like so:
for i, v in pairs(character:GetChildren()) do
if v:IsA("Hat") then -- Most modern hats are "Accessory"s now. A different method would be to set a custom attribute called Hats or something, and if true, delete that hat.
v:Destroy()
end
end
Keep in mind, this is an example. This code assumes all of your hats are a “Hat” instance.