How do I undefine a variable in CF 5.0?

How can I completely remove/annihilate/undefine a defined variable that would cause it to then fail an 'IsDefined()' test?


<cfset Application.TempVar = "test">

<!--- do what? to cause this if test to fail? --->

<cfif IsDefined( "Application.TempVar" )>
   <!--- want to wind up HERE --->
   <cfoutput>but I thought it existed!</cfoutput>

Remember, CF5! Thanks!
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I can't think of a reason to use isDefined(). There's an alternative: structKeyExists(). And since all variables are scoped in ColdFusion (even if you don't define a scope, it ends up in the variables scope) and structKeyExists() is a tiny bit faster than isDefined(), why not use it all the time?

So, instead of IsDefined("Application.TempVar"), use structKeyExists(application, "tempVar").
And to answer your question: structDelete(application, "tempVar") will remove, annihilate, undefine and even delete the key from the application struct.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
my little workaround to that problem is to check if the variable exists and to check it a value for it exists

<CFIF IsDefined( "Application.TempVar" )> AND Application.TempVar NEQ "">

or something to that affect
tbaumAuthor Commented:
mosphat, GOOD ANSWER, now do me one better...

You will get the points, now for the bonus round...

That will work find for Application and Session vars, but what about just plan-old local vars?  How would you go about destroying them?

Like I said: even if you don't define a scope, it ends up in the variables scope.

This variable is actually set in the variables scope:
<cfset plain_old_local_var = "test">

So you can delete it like this:
<cfset structDelete(variables, "plain_old_local_var")>
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Servers

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.