Director James Gunn attends the European launch of Marvel Studios’ “Guardians of the Galaxy Vol. 2” at the Eventim Apollo last year in London. (Ian Gavan/Getty Images for Disney)

AS A MAINSTREAM Disney director of family entertainment, there’s no coming back from this.

James Gunn, the director of the first two films in Disney/Marvel’s $1.6 billion “Guardians of the Galaxy” franchise, was fired Friday from “Guardians of the Galaxy Vol. 3” after what Disney called “indefensible” tweets resurfaced this week.

On Thursday, Gunn apologized in several tweets to his nearly half-million Twitter followers, trying to explain that he attempted such social media “jokes” back when he viewed himself as “a provocateur” whose humor and horror movies alike were “outrageous and taboo.”

Joking in a taboo manner about such subjects as rape and pedophilia didn’t hurt Gunn’s filmmaking career back when he was a lesser-known indie director releasing low-budget, industry-admired movies like “Slither.” But now that he is mainstream Disney, there is, of course, no way a major studio creating all-ages entertainment can keep even a beloved franchise director in a leadership role.

Walt Disney Chairman Alan Horn said Friday in a statement: “The offensive attitudes and statements discovered on James’ Twitter feed are indefensible and inconsistent with our studio’s values, and we have severed our business relationship with him.”

The irony here, of course, is that Disney simply had to know about Gunn’s history of offensive online remarks when it hired him for 2014’s “Guardians of the Galaxy” — the first film in a franchise powered largely by Gunn’s bent sense of humor and love of classic rock songs.

Gunn told me last year that part of what he loved about working for Marvel was the studio’s dedication to identifying and hiring filmmakers who have quirky visions combined with the ability to deliver a blockbuster movie. “It’s a wonderful talent that they [the executives] have, and I hope that they keep making smart choices,” Gunn told Comic Riffs, citing the faith of Marvel chief Kevin Feige.

And Disney had rewarded Gunn’s ability to deliver popular, profitable superhero movies with a distinctive viewpoint by handing him the second “Guardians” sequel. Gunn told The Post last year that he looked forward to completing a great trilogy, with “Vol. 3” set to be released in 2020, as adapted from comics by Dan Abnett and Andy Lanning.

But again, Disney had to know about the risks of handing such huge reins to a director who stirred controversy for his online writing six years ago, when the Mary Sue resurfaced his darker posts, including a 2011 blog post that included a poll asking readers which superheroes they most wanted to have sex with. Even Gunn’s defenders at the time — who said they could make an argument that his writing was over-the-top parody or bad satire — called his words “misogynistic and homophobic.”

In his 2011 blog post, Gunn — in remarking on the appeal of superheroes in graphic detail — wrote of gay and lesbian sex, underage sex and other sexual acts in ways that offended many readers.

Some of the commenters, calling Gunn “creepy” and “sickening” at the time, said they did not look forward to a “Guardians” franchise guided by a filmmaker who would post such passages, even in jest as a provocateur.

Disney has endured some of its directors and writers, including creatives with Star Wars, being overtly political online — as Gunn has been, as well. But in an age when nothing on social media seems to disappear forever — particularly if written by a prominent person — Disney had to be concerned that some of Gunn’s past “jokes” would resurface and be radioactive.

Gunn said in a statement Friday afternoon about his resurfaced tweets: “I have regretted them for many years since — not just because they were stupid, not at all funny, wildly insensitive, and certainly not provocative like I had hoped, but also because they don’t reflect the person I am today or have been for some time.”

That said, Gunn probably has a creative future if he wants to return to indie filmmaking.

But Gunn, like Disney, had to know the deal if this day ever came.

One of Gunn’s “Guardians of the Galaxy” stars reacts:

Read more:

#WeAreGroot: How Hollywood is reacting to James Gunn’s Disney firing

‘Guardians of the Galaxy Vol. 3’ director James Gunn fired by Disney over old, offensive tweets