unset($a->new_property);

This works for array elements, variables, and object attributes.

Example:

$a = new stdClass();

$a->new_property = 'foo';
var_export($a);  // -> stdClass::__set_state(array('new_property' => 'foo'))

unset($a->new_property);
var_export($a);  // -> stdClass::__set_state(array())
Answer from Yanick Rochon on Stack Overflow
🌐
PHP
php.net › manual › en › function.unset.php
PHP: unset - Manual
Objects will only free their resources and trigger their __destruct method when *all* references are unsetted. Even when they are *in* the object... sigh! <?php class A { function __destruct() { echo "cYa later!!\n"; } } $a = new A(); $a -> a = $a; #unset($a); # Just uncomment, and you'll see echo "No Message ...
🌐
Quora
quora.com › How-can-I-destroy-an-Object-in-PHP
How to destroy an Object in PHP - Quora
Answer (1 of 21): A2A There are 2 ways; 1. Set the object variable to [code ]null[/code] : this will set the object and its reference both to null, which is in my opinion is more efficient than using the unset function. 2. Use [code ]unset( )[/code] ...
🌐
Learning About Electronics
learningaboutelectronics.com › Articles › How-to-delete-an-object-in-PHP.php
How to Delete an Object In PHP
So with the PHP unset() function, putting the object that we want to delete as the parameter to this function, we can delete this object.
🌐
GitHub
github.com › phpstan › phpstan-strict-rules › issues › 170
unset() on already initialised object property? · Issue #170 · phpstan/phpstan-strict-rules
April 7, 2022 - <?php declare(strict_types = 1); class A { public string $x = ''; } $a = new A; unset($a->x); I would consider property $x to be initialized, there I would not expect it to become uninitialised again. I think it should NOT be allowed to unset value from object outer scope.
Published   Apr 07, 2022
🌐
Purpleturtlecreative
purpleturtlecreative.com › home › blog › how to recursively unset array keys and object properties in php
How to Recursively Unset Array Keys and Object Properties in PHP
September 10, 2024 - */ function deep_unset_prop( array|object &$data, string $prop ) { if ( is_object( $data ) ) { unset( $data->{$prop} ); } foreach ( $data as &$value ) { if ( is_array( $value ) || is_object( $value ) ) { deep_unset_prop( $value, $prop ); } } }Code language: PHP (php)
🌐
LornaJane
lornajane.net › posts › 2017 › removing-object-properties-before-var_dumping-them
Removing Object Properties Before Var_Dumping Them | LornaJane
This magic method will be called automatically when PHP encounters a call to var_dump() or print_r() with an object of this class as the target. For my example, I just wanted to remove a property called “client” from the object, so the code looks like this: public function __debugInfo() { // remove the $client property because the output is HUGE $result = get_object_vars($this); unset($result['client']); return $result; }
🌐
TutorialsPoint
tutorialspoint.com › php-remove-object-from-array
PHP: Remove object from array
$index = 2; $objectarray = array( 0 => array('label' => 'abc', 'value' => 'n23'), 1 => array('label' => 'def', 'value' => '2n13'), 2 => array('label' => 'abcdef', 'value' => 'n214'), 3 => array('label' => 'defabc', 'value' => '03n2') ); var_dump($objectarray); foreach ($objectarray as $key => $object) { if ($key == $index) { unset($objectarray[$index]); } } var_dump($objectarray);
Top answer
1 of 2
1

No, you do not need to delete an object after its use in PHP. Let PHP's garbage collection take care of it for you.

There may be some obscure performance reasons to unset an object after you are done with it in very specific situations. But such cases are not everyday events, and they will be clear when you see them.

2 of 2
1

PHP's GC was hit and miss prior to 5.3 with many arrays and objects becoming unfreeable until the script ended. Because of this, PHP development basically took on an unspoken “who needs GC anyways” attitude because vars were only guaranteed to be freed when the script ended.

Two things are happening which change this:

First, PHP's GC has advanced to where its only unreliable with arrays that contain copies of other arrays. PHP's GC will be totally functional and reliable once this issue is fixed .

Secondly, PHP's pthread extension is coming along pretty well. It's very likely that PHP will have full thread support within the next couple years, making long running scripts as usefull as many Java appliations.

These two things are likely to change many standards in the way PHP is used. For example, your already starting to see serious efforts to create PHP based network services (such as Ratchet) now that the GC is reliable under most circumstances.

So to make a long story short - no, your currently not required to unset any form of resources in PHP because they will all be freed when the script ends. However, unsetting objects and other resources will make sure that each unneeded resource is freed before the script ends, potentially freeing up highly contented resources and thus improve performance.

Moreover, the practice of unsetting objects and resources is likely to become the norm in the near future, and likely an absolute requirement in many cases.

Note that many PHP objects/resources don't fall out of scope until the script ends. This means that unset() is the only way to ensure that their ref count is set to 0 in order to become a candidate for freeing by the GC (before the script ends, that is.)

Find elsewhere
🌐
BCCNsoft
doc.bccnsoft.com › docs › php-docs-7-en › function.unset.html
Unset a given variable
<?php $name = 'Felipe'; var_dump((unset) $name); var_dump($name); ?> ... Note: Because this is a language construct and not a function, it cannot be called using variable functions. ... It is possible to unset even object properties visible in current context.
🌐
W3Docs
w3docs.com › php
PHP : Remove object from array
<?php $array = [new stdClass(), new stdClass(), new stdClass()]; $objectToRemove = new stdClass(); $key = array_search($objectToRemove, $array); if ($key !== false) { unset($array[$key]); echo "Object removed from the array!"; } else { echo "Object not found in the array."; } ?>
🌐
Scaler
scaler.com › home › topics › php unset() function
PHP unset() Function - Scaler Topics
September 29, 2023 - Run the above code in your editor for a better and clear explanation. unset() is used to free up memory by unsetting variables or removing elements from arrays, allowing efficient memory management in PHP applications.
🌐
W3Schools
w3schools.com › php › func_var_unset.asp
PHP unset() Function
The unset() function unsets a variable. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, ...
🌐
Exakat
php-dictionary.readthedocs.io › en › latest › dictionary › unset.ini.html
unset() — PHP Dictionary 1.0.145 documentation
January 26, 2026 - unset() removes a variable, an array item or an property. This feature used to be available as a function call unset() or as a type cast (unset). The type-cast was removed in PHP 7.2.
🌐
Educative
educative.io › answers › what-is-the-php-unset-function
What is the PHP unset() function?
The unset() function in PHP resets any variable. If unset() is called inside a user-defined function, it unsets the local variables. If a user wants to unset the global variable inside the function, then he/she has to use $GLOBALS array to do so.