A thesaurus may suggest ‘optional’ as an antonym for ‘necessary’ but this isn't the word I'm looking for. I need a word which means ‘cannot be included’ as in the phrase, “The country code is necessary when dialing from outside Colombia, but cannot be included when dialing from within Colombia.” In this sense, the country code is certainly not optional—if it is included the call won't go through. (This is actually true for mobile phone numbers in Colombia. It makes specifying phone numbers on calling cards or web contact pages a bitch.)
The country code is necessary when dialing from outside Colombia, but is prohibited when dialing from within Colombia.
The one downside of "prohibited" is that it seems to imply that there is some other person who is stopping you from doing something. "necessary" has none of this context. I wonder if there is some perfect word I can't think of that doesn't have this flaw.
There are a number of words with the connotation you desire, but I'm not sure there is one with such a denotation. However, the following come to mind:
forgo: omit, decline to take, go without, abstain from, refrain from, eschew
exclude: shut out, deny access to, bar from a place, group, or privilege; Please exclude this person from your list.
**prohibit: formally forbid by law, rule, or other authority, forbid, ban, bar, interdict, disallow, prevent, stop, rule out,
preclude: to prevent something from happening or to prevent someone from doing something; This action precludes the necessity of having to agree on names.
eliminate:completely remove or get rid of; remove, get rid of, put an end to, do away with, end, stop
Then there's always "Please do not include the country code."
It seems (from the responses I've seen so far) that there's no perfect antonym for "necessary" in the sense you mean.
I like Kris' answer with "excluded", but the the obvious antonym of "excluded" is "included" and the usual antonym of "necessary" is "unnecessary", so the contrast between "necessary" and "excluded" is a little weird.
Maybe is not a good idea to build your sentence as a contrast if you do not count with two clear contrasting elements.
Actually both your original phrase and the options offered so far are quite confusing. You have to read those phrases several times to be sure of its meaning.
You could just write someting shorter and much clearer like:
Use the country code only when dialing from outside Colombia.
The country code is necessary when dialing from outside Colombia, but excluded when dialing from within Colombia.
If the country code exists in the number, it is stripped (removed/ excluded) before entering. If it does not exist in the number, it is considered as 'not necessary' (absence ignored/ excluded).