How do I undefine a variable in CF 5.0?

Posted on 2004-11-11
Last Modified: 2013-12-24
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!
Question by:tbaum
    LVL 6

    Accepted Solution

    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.

    LVL 5

    Expert Comment

    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
    LVL 1

    Author Comment

    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?

    LVL 6

    Expert Comment

    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")>

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Article by: kevp75
    Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
    Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now