A US judge has ordered the immediate release of Chelsea Manning, the former American army officer who was remanded to prison after refusing to testify against WikiLeaks.

The ruling states that it is no longer necessary for her to testify and follows her attorneys’ announcement that she had recently tried to kill herself while imprisoned. She is reportedly recovering in hospital.

Ms Manning spent seven years in a military prison after leaking thousands of classified government documents to WikiLeaks before Barack Obama commuted her sentence in 2017.

Last year, she was held in contempt of court after refusing to testify before a federal grand jury as part of an investigation into Julian Assange and WikiLeaks. She has been jailed since May.

Mr Assange is fighting extradition from the UK to Virginia, where he faces charges under the Espionage Act.

In 2013, Ms Manning was charged with several offences following a leak of military and diplomatic documents, including videos of bombings in Iraq and Afghanistan, and was sentenced to 35 years in prison after her conviction.

As a low-level intelligence analyst in the army with access to a classified computer system, Ms Manning retrieved thousands of documents spanning the wars in Iraq and Afghanistan, exposing abuses of detainees and attacks – including the “Collateral Murder” video in which two journalists are killed by a helicopter strike – in addition to sensitive conversations among intelligence officials. Her role in “Cablegate” revealed thousands of diplomatic cables across several decades.

Protesters urge Obama to commute Chelsea Manning's sentence Show all 12 1 /12 Protesters urge Obama to commute Chelsea Manning's sentence Protesters urge Obama to commute Chelsea Manning's sentence torbakhopper/Flickr Protesters urge Obama to commute Chelsea Manning's sentence Garry Knight/Flickr Protesters urge Obama to commute Chelsea Manning's sentence People hold signs calling for the release of imprisoned wikileaks whistleblower Chelsea Manning while marching in a gay pride parade in San Francisco, California June 28, 2015. Manning has appealed to an Army court to overturn her court-martial conviction, a court filing released on Thursday said. Reuters Protesters urge Obama to commute Chelsea Manning's sentence Around twenty activists associated with the Chelsea Manning support group rally in front of the White House in Washington DC, Sunday May 11, 2014. Stephen Melkisethian/Flickr Protesters urge Obama to commute Chelsea Manning's sentence Daily Chalkupy/Flickr Protesters urge Obama to commute Chelsea Manning's sentence Around twenty activists associated with the Chelsea Manning support group rally in front of the White House in Washington DC, Sunday May 11, 2014. Protesters urge Obama to commute Chelsea Manning's sentence Twitter Protesters urge Obama to commute Chelsea Manning's sentence Abigail Edward holds up a sign advocating the release of WikiLeaks whistle blower Chelsea Manning along the Gay Pride parade route in San Francisco, California on Sunday, June, 26, 2016. Josh Edelson/AFP/Getty Images Protesters urge Obama to commute Chelsea Manning's sentence Demonstration for Chelsea Manning in London, England, United Kingdom. Chelsea Manning (born Bradley Edward Manning) is a United States Army soldier who was convicted by court-martial in July 2013 of violations of the Espionage Act and other offenses, after disclosing to WikiLeaks nearly three-quarters of a million classified or unclassified but sensitive military and diplomatic documents. Manning was sentenced in August 2013 to 35 years imprisonment, with the possibility of parole in the eighth year, and to be dishonorably discharged from the Army. Manning is a trans woman who, in a statement the day after sentencing, said she had felt female since childhood, wanted to be known as Chelsea, and desired to begin hormone replacement therapy. From early life and through much of her Army life, Manning was known as Bradley; she was diagnosed with gender identity disorder while in the Army. Mike Kemp/Getty Protesters urge Obama to commute Chelsea Manning's sentence Chelsea M Queer Friends/Twitter Protesters urge Obama to commute Chelsea Manning's sentence Instagram Protesters urge Obama to commute Chelsea Manning's sentence Jil Love Revolution/Instagram

WikiLeaks, in concert with several news organisations, published the documents. Ms Manning said she hoped to ignite a global discussion about the US military’s presence in the Middle East and subsequent reforms, though her whistle-blowing ultimately led to her prosecution – and a charge of aiding the enemy, which could have resulted in her execution if convicted.

During her imprisonment and after her release, Ms Manning has been an outspoken advocate for government transparency and transgender rights and healthcare.

In his ruling on Thursday, Judge Anthony Trenga did not waive the $256k (£200k) in penalties levied against her. The ruling says that enforcement of the “accrued, conditional fines would not be punitive but rather necessary to the coercive purpose” of the court’s contempt order.