Two witnesses go to the beit din and claim that so and so violated the sabbath on a biblical level. The witnesses claim they warned him in accordance with the rules and they did their part as witnesses. When so and so is brought to beis din, he claims he did a melacha (such as digging a hole), but he claims it was for the dirt. Which would only be a rabbinical prohibition because it's a מלאכה שאין צריכה לגופה. If a person were to use this as their defense to deflect the death penalty would it theoretically work?
The Maharsha (Bava Basra 119a) discusses the case of the m'kosheish eitzim (the man who gathered wood on the Sabbath, see B'midbar 15:32-36). The Maharsha mentions that, according to Targum Yonasan (B'midbar 15:32), the man was not gathering wood for actual use. Instead, he was gathering wood in order to precipitate a divine revelation of the appropriate form of death penalty for a Sabbath violator.1
The Maharsha asks how the m'kosheish could have been liable to the death penalty according to Rabbi Shim'on, who exempts a m'lacha she'eina tz'richa l'gufa2 from liability. After all, the m'kosheish gathered the wood with an intent unrelated to the wood itself. The Maharsha answers that even though the m'kosheish was theoretically exempt according to R' Shim'on, he was still liable to be killed in a court since the witnesses were not made aware of his intentions at the time that they warned him:
ומיהו הוא ודאי דהיה חייב מיתה בדיני אדם שלא ידעו העדים שהתרו בו שהוא עשה על דעת זו ואינן אלא דברים שבלב ודנין היו אותו למיתה ע"פ העדות
However, he was certainly liable for a court imposed death penalty, since the witnesses that warned him did not know that the violator performed the act with this intention, and his intention constitutes only unspoken thoughts [of which the witnesses were unaware, and which have no legal significance to the court], and they sentenced him to death based on the testimony.
Apparently, had he informed the witnesses initially that he was performing a m'lacha she'eina tz'richa l'gufa, he could not have been held liable in court (according to Rabbi Shim'on).3 Perhaps the Maharsha would even concede that a person would be exempt from punishment if the circumstances at the time of the m'lacha act suggest it was likely a m'lacha she'eina tz'richa l'gufa.
1 Alternatively, according to Tosafos (Bava Basra 119b, s.v. אפילו קטנה) he was demonstrating that the rest of the generation that was already condemned to a heavenly death penalty (for accepting the report of the spies, see B'midbar 14:20-35) was still obligated to follow the commandments.
2 Forbidden labor performed for some purpose other than the type of purpose for which that labor was performed in the construction of the mishkan (or also, according to some opinions, performed for the service of the mishkan). For example, digging a hole to get dirt rather than to create a furrow would be eina t'zricha l'gufa.
3 Though in the particular instance of the m'kosheish eitzim, as understood by those sources that say he was trying to get himself executed, such an exculpatory admission would have defeated his purpose.
תנו רבנן מכירים אתם אותו נכרי הרג ישראל הרג התריתם בו קיבל עליו התראה התיר עצמו למיתה המית בתוך כדי דיבור
(paraphrased) They ask the witnesses, "did he accept the warning and accept that he would be killed?"
Rashi on התיר עצמו למיתה:
התיר עצמו למיתה - שכשהתריתם בו אל תעבור שאתה מתחייב מיתה פלונית הוא אומר על מנת כן אני עושה דאם לא הפקיר עצמו אינו נהרג כדלקמן
When you warned him, you said "do not violate this because you will get a death penalty for it" and he said "I am doing it on that condition" - if he does not accept that he will be killed for doing it, he is not killed
Part of his warning was his acknowledgement that he will be killed for what he is about to do. Additionally, the warning must contain the specific prohibition that he is violating, complete with the name of the Av or Toldah of the Melacha (Tosefos based on Shabbos 138a). So he admitted exactly what act he was performing as part of accepting his warning, and he cannot deny later that he was doing exactly what he admitted to two witnesses that he was doing.