How to remove all the SKSpriteNodes created with a spawning function?

In the game I’am working on I wrote a spawnCoins function to reward the successful progress of the player:

   `//Spawning Coins - Estrellitas
 
    func spawningCoin (posicionX: CGFloat) {
    
    let spawnCoin = SKSpriteNode(imageNamed: "coin")
    
    spawnCoin.position = CGPoint(x: posicionX, y: 1476.0)
    spawnCoin.zPosition = 51
    spawnCoin.name = "spawnCoin"
    addChild(spawnCoin)
}`

But, if the player fails in some point I want all his coins to dissapear from the scene.
That’s something I can’t achieve. Everything I tried is useless, the coins remain in the scene.

I’am sure there is a solution but I’m unable to find it.

You could tag them and just remove all sprites with that name group…

“When choosing a name for a node, decide whether each node gets a unique name or whether some nodes will share a common name. If you give the node a unique name, you can find the node later by calling the childNodeWithName: method. If a name is shared by multiple nodes, the name usually means that these are all a similar object type in your game. In this case, you can iterate over those objects by calling the enumerateChildNodesWithName:usingBlock: method.”

taken from here:

https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKNode_Ref/index.html#//apple_ref/occ/instp/SKNode/name

Hi, thank you very much for your help.
Sorry for ask you this but… how this method needs to be constructed? I look at your link but after several tries I can’t make it work.

Finally I have learned how it works!
Here it is my solution:

`self.enumerateChildNodesWithName("spawnCoin") {
        node, stop in
        node.runAction(SKAction.removeFromParent())
 }`

Thanks marciokoko for your help