Heat oven to 220C/200C fan/gas 7. Boil potatoes for 15 mins until tender, then drain well and smash a little. Cool slightly.
Mix the cheese, cayenne and onions with the smashed potatoes and stir to make a mouldable filling. Roll out the pastry to a rectangle about 25 x 35cm. Carefully lift onto a baking sheet and brush all over with beaten egg.
Press the cheese mix along one of the long sides of the pastry, leaving 2cm pastry free on one side for sealing, and half of the pastry empty on the other side for folding over later. Fold the pastry over the filling and press the edges together, before trimming with a knife and sealing well by pressing the edges with a fork.
Brush with egg and bake for 30 minutes Cool, then slice and serve with caramelised red onions. Can be made 1 day ahead.