The Soviets had captured Berlin, and on 30 April Hitler had committed suicide. He had appointed Grand Admiral Karl Dönitz as Head of State and Supreme Commander of the Armed Forces. US Forces from the west and Soviet forces from the east had already met at Torgau, cutting the Reich in two, while in the north 21st Army Group was poised to capture Hamburg and the other German ports.
The state of the German navy, the Kriegsmarine, was no better. Of its capital ships only Prinz Eugen survived, sheltering at Copenhagen; only the U-Boat Arm was capable of continuing the fight. The Kriegsmarine had approximately 470 U-boats remaining. Some 170 of these were operational U-boats (Front boats), based mainly in occupied Norway, and another 200 home-based boats in various stages of building, commissioning and working up; these were mainly in the north German ports and on the Baltic.
As head of the Kriegsmarine, and as commander of the U-boat arm, Dönitz was keen that his U-boat force should not be surrendered. However, as the new German leader, he was keen to extricate Germany from the war and, if possible, avoid Allied, particularly Soviet, retribution. To that end he had opened negotiations with the western Allies, through Field Marshal Montgomery commander of Allied 21 Army Group, in North Germany.
As the Allies closed in on the North German ports the Kriegsmarine started to destroy what was left to prevent its capture, while all serviceable boats were ordered to bases in Norway. During May a final massacre of U-boats fleeing to Norway took place; 23 U-boats were destroyed or damaged beyond repair in transit in the first week of May.
Against this backdrop, Dönitz and the U-boat arm made plans for a mass scuttle of his U-boats, to be carried out on receiving the code-word "Regenbogen".
During May the Kriegsmarine started to scuttle its U-boats ahead of the advancing Allied armies.
On 3 May, Dönitz sent his chief aide von Friedeburg to Montgomery at Lüneburg to open negotiations for an armistice with the western allies. This was refused, as von Friedeburg was not empowered to agree to an unconditional surrender, upon which the Allies insisted. Also on 3 May another 39 boats were wrecked, 32 at Kiel and another 7 at Hamburg, on the North Sea coast.
On 4 May the Supreme Allied Commander, Eisenhower allowed the German forces in Northwest Europe, including naval forces, to surrender to Montgomery and 21 Group; for this, Montgomery insisted that German naval forces, including the U-Boat arm, be surrendered intact. This would come into effect at 8 am on 5 May. Meanwhile, on 4 May, four more boats, two in the Kiel Canal and two at Flensburg, were scuttled.
In the early hours of 5 May, the Regenbogen order was given, only to be countermanded 8 minutes later, to avoid jeopardizing the surrender negotiations, and later that day all operational U-boats were ordered to cease hostilities. Despite this a further 87 boats were destroyed on 5 May; 64 on the Baltic (41 at Gelting Bay, 13 at Flensburg and 10 at various other points), while on the North Sea coast 23 boats were disposed of, 13 at Wilhelmshaven and 10 in the Weser estuary.
On 8 May, Germany surrendered unconditionally; the remaining naval units, including the surviving U-boats, surrendered to Allied forces. At least 150 U-boats were surrendered to the Allied navies, either at sea or at their operational bases. 52 boats were surrendered at sea, either on patrol or in transit, and 98 in port, mostly in Norway and at bases in Germany, Denmark and France. Four U-boats and their crews fled to neutral ports; two (U-1277 and U-963) to Portugal and two (U-530 and U-977) to Argentina.
Various figures are given for the numbers of U-boats involved during this period; Kemp gives 218 scuttled, and 154 surrendered; Tarrant gives the same. Blair gives 222 scuttled and 174 surrendered. Neistle lists 195 scuttled  and 150 surrendered. A number of U-boats disposed of were not in commission; some had not yet been commissioned, some had been decommissioned. The discrepancies are mainly accounted for depending on whether these are included or not.
Most sources  give a number of U–boats scuttled at the end of the war, and describe the Regenbogen order, conflating the two. Kemp describes the scuttle as an act of defiance, and quotes "Ali" Cremer's order to scuttle U-2519 rather than surrender her.
It is questionable, however, to what extent the effect the order had, or even if it was given at all. To take Cremer's case in point, Neistle lists U-2519 as being scuttled on 3 May at Kiel, before the surrender negotiations were complete, and at least 24 hours before the Regenbogen order was given.
Blair on the other hand describes the order as being given "according to some sources", and queries "whether it was true or not", but believes some "ambiguous orders of some kind" were issued. But he also states the scuttle began on 5 May, while Neistle is clear it started 4 days earlier, at beginning of the month.  
Certainly by 1 am on 5 May at least 76 boats had already been wrecked, about half the total. On 5 May, and subsequently, another 89 were wrecked/scuttled, all in North German ports.
Neistle gives the total of U-boats scuttled as 195, of which half were destroyed before, and half after, the Regenbogen order was given. Of the boats destroyed, most (184) were non-operational "Home" boats in North German ports. 11 were Front boats, and these include those fatally damaged in the May massacre and subsequently scuttled by their crews.
On 8 May 1945, the surviving U-boats were surrendered to the Allied navies, either at sea or at their operational bases in Norway and on the North Sea coast, ending the Kriegsmarine's war at sea.
- van der Vat p380
- Kemp p262
- Tarrant p228
- Blair p700
- Neistle p 198-99
- Neistle p 207-209
- Kemp, Roskill, Tarrant
- Kemp p 261
- Neistle p 160
- van der Vat p380
- Blair p700
- Blair p 700
- Neistle p 198-99
- Neistle p 202
- Clay Blair : Hitler's U-Boat War [Volume 2]: The Hunted 1942-1945 (1998) ISBN 0-304-35261-6 (2000 UK paperback ed.)
- Paul Kemp : U-Boats Destroyed ( 1997) . ISBN 1-85409-515-3
- Axel Neistle : German U-Boat Losses during World War II (1998). ISBN 1-85367-352-8
- Stephen Roskill : The War at Sea 1939-1945 Vol III, pt 2 (1960). ISBN (none)
- VE Tarrant : The Last Year of the Kriegsmarine (1994) ISBN 1-85409-176-X
- Dan van der Vat : The Atlantic Campaign (1988).ISBN 0 340 37751 8