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:
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.
Rashi on התיר עצמו למיתה:
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.