Magento – Deleting an address and relationship with customer

customercustomer-addressmagento-1.9.2.1

When fetching a customer's addresses and deleting them in a foreach loop:

$addresses = $customer->getAddresses();

foreach ($addresses as $address) {
    $address->delete();
}

The addresses appear empty in the customer's address book on the frontend and also in the customer admin section. So the address has been deleted, but the relation to the customer still exists.

When taking a look at Mage_Customer_AddressController::deleteAction nothing more seems to happen than what I am doing, but the addresses don't seem to appear as empty addresses anymore.

What am I overlooking?

Best Answer

Just an idea... try

$addresses = $customer->getAddresses();

foreach ($addresses as $address) {
    $address->setCustomerId(null)->save();
    $address->delete();
}
Related Topic