Index: kernel/classes/datatypes/ezimage/ezimagealiashandler.php
===================================================================
--- kernel/classes/datatypes/ezimage/ezimagealiashandler.php	(revision 70252)
+++ kernel/classes/datatypes/ezimage/ezimagealiashandler.php	(working copy)
@@ -713,123 +713,69 @@
     }
 
     /**
-     * Removes all the image aliases and their information.
-     * The stored images will also be removed if the attribute is the owner
-     * of the images.
+     * Removes aliases from the attribute.
      *
-     * After the images are removed the attribute will containe an internal
-     * structure with empty data
-     *
-     * @param eZContentObjectAttribute $contentObjectAttribute
-     *        Content object attribute to remove aliases for
-     *
-     * @return void
+     * Files, as well as their references in ezimagefile, are only removed if this attribute/version was
+     * the last one referencing it. The attribute's XML is then reset to reference no image.
      */
-    function removeAliases( $contentObjectAttribute )
+    function removeAliases()
     {
-        $aliasList = $this->aliasList();
-        $alternativeText = false;
+        foreach ( $this->aliasList() as $alias )
+        {
+            if ( $alias['is_valid'] )
+                $this->removeAliasFile( $alias['url'] );
+        }
 
-        $contentObjectAttributeVersion = $this->ContentObjectAttributeData['version'];
-        $contentObjectAttributeID = $this->ContentObjectAttributeData['id'];
+        $this->generateXMLData();
+    }
 
-        $isImageOwner = $this->isImageOwner();
+    /**
+     * Removes $aliasFile and references to it for the attribute.
+     *
+     * The eZImageFile entry as well as the file are only removed if this attribute was the last reference.
+     *
+     * @param string $aliasFile
+     *
+     * @return array bool true if something was actually done, false otherwise
+     */
+    public function removeAliasFile( $aliasFile )
+    {
+        if ( $aliasFile == '' )
+            throw new InvalidArgumentException( "Expecting image file path" );
 
-        // We loop over each image alias, and look up the file in ezcontentobject_attribute
-        // Only images referenced by one version will be removed
-        foreach ( $aliasList as $aliasName => $alias )
+        // We only delete the eZImageFile if it isn't referenced by attributes of the same id but different version/language
+        if ( eZImageFile::isReferencedByOtherAttributes(
+            $aliasFile,
+            $this->ContentObjectAttributeData['id'],
+            $this->ContentObjectAttributeData['version'],
+            $this->ContentObjectAttributeData['language_code'] ) )
         {
-            $dirpath = $alias['dirpath'];
-            $doNotDelete = false; // Do not delete files from storage
-
-            if ( $aliasName == 'original' )
-                $alternativeText = $alias['alternative_text'];
-            if ( $alias['is_valid'] )
-            {
-                $filepath = $alias['url'];
-
-                /**
-                 * If there are no ezimagefile references to this file with this content object attribute id,
-                 * we are dealing with an attribute that was copied from another content.
-                 * The image doesn't belong to us, and we just don't do anything.
-                 * See http://jira.ez.no/browse/EZP-21324
-                 */
-                if ( !eZImageFile::fetchByFilepath( $contentObjectAttributeID, $filepath ) )
-                    continue;
-
-                // Fetch ezimage attributes that use $filepath
-                // Always returns current attribute (array of $contentObjectAttributeID and $contentObjectAttributeVersion)
-                $dbResult = eZImageFile::fetchImageAttributesByFilepath( $filepath, $contentObjectAttributeID );
-                $dbResultCount = count( $dbResult );
-                // Check if there are the attributes.
-                if ( $dbResultCount > 0 )
-                {
-                    $doNotDelete = true;
-                    foreach ( $dbResult as $res )
-                    {
-                        // We only look results where the version matches
-                        if ( $res['version'] == $contentObjectAttributeVersion )
-                        {
-                            // If more than one result has been returned, it means
-                            // that another version is using the same image,
-                            // and we should not delete this file
-                            if ( $dbResultCount > 1 )
-                            {
-                                continue;
-                            }
-                            // Only one result means that the current attribute
-                            // & version are the only ones using this image,
-                            // and it can be removed
-                            else
-                            {
-                                $doNotDelete = false;
-                            }
-                        }
-
-                        eZImageFile::appendFilepath( $res['id'], $filepath, true );
-                    }
-                }
-
-                if ( !$doNotDelete )
-                {
-                    $file = eZClusterFileHandler::instance( $filepath );
-                    if ( $file->exists() )
-                    {
-                        $file->delete();
-                        eZImageFile::removeFilepath( $contentObjectAttributeID, $filepath );
-                        eZDir::cleanupEmptyDirectories( $dirpath );
-                    }
-                    else
-                    {
-                        eZDebug::writeError( "Image file $filepath for alias $aliasName does not exist, could not remove from disk", __METHOD__ );
-                    }
-                }
-            }
+            return;
         }
 
-        $doc = new DOMDocument( '1.0', 'utf-8' );
-        $imageNode = $doc->createElement( "ezimage" );
-        $doc->appendChild( $imageNode );
-
-        $imageNode->setAttribute( 'serial_number', false );
-        $imageNode->setAttribute( 'is_valid', false );
-        $imageNode->setAttribute( 'filename', false );
-        $imageNode->setAttribute( 'suffix', false );
-        $imageNode->setAttribute( 'basename', false );
-        $imageNode->setAttribute( 'dirpath', false );
-        $imageNode->setAttribute( 'url', false );
-        $imageNode->setAttribute( 'original_filename', false );
-        $imageNode->setAttribute( 'mime_type', false );
-        $imageNode->setAttribute( 'width', false );
-        $imageNode->setAttribute( 'height', false );
-        $imageNode->setAttribute( 'alternative_text', $alternativeText );
-        $imageNode->setAttribute( 'alias_key', false );
-        $imageNode->setAttribute( 'timestamp', false );
+        eZImageFile::removeObject(
+            eZImageFile::definition(),
+            array(
+                'contentobject_attribute_id' => $this->ContentObjectAttributeData['id'],
+                'filepath' => $aliasFile
+            )
+        );
 
-        $this->ContentObjectAttributeData['DataTypeCustom']['dom_tree'] = $doc;
-        unset( $this->ContentObjectAttributeData['DataTypeCustom']['alias_list'] );
+        // We only remove the image file if other eZImageFile of the same attribute use it (other versions)
+        if ( eZImageFile::isReferencedByOtherImageFiles( $aliasFile, $this->ContentObjectAttributeData['id'] ) )
+        {
+            return;
+        }
 
-        $this->storeDOMTree( $doc, true, $contentObjectAttribute );
+        // finally, remove file if applicable
+        $file = eZClusterFileHandler::instance( $aliasFile );
+        if ( !$file->exists() )
+        {
+            eZDebug::writeError( "Image file {$aliasFile} does not exist, could not remove from disk", __METHOD__ );
+            return;
+        }
+        $file->delete();
+        eZDir::cleanupEmptyDirectories( dirname( $aliasFile ) );
     }
 
     /*!
Index: kernel/classes/datatypes/ezimage/ezimagefile.php
===================================================================
--- kernel/classes/datatypes/ezimage/ezimagefile.php	(revision 70253)
+++ kernel/classes/datatypes/ezimage/ezimagefile.php	(working copy)
@@ -117,7 +117,7 @@
         );
         // Escape _ in like to avoid it to act as a wildcard !
         $filepath = addcslashes( $filepath, "_" );
-        $query = "SELECT id, version
+        $query = "SELECT id, version, language_code
                   FROM   ezcontentobject_attribute
                   WHERE  contentobject_id = $contentObjectID and
                          contentclassattribute_id = $contentClassAttributeID and
@@ -146,6 +146,25 @@
                                                 $asObject );
     }
 
+    /**
+     * Fetches unique eZImageFile data (as an array) for $filePath
+     *
+     * @param string $filePath
+     *
+     * @return array array of hash. Keys: contentobject_attribute_id, filepath
+     */
+    static function fetchListByFilePath( $filePath )
+    {
+        return eZPersistentObject::fetchObjectList(
+            eZImageFile::definition(),
+            array( 'contentobject_attribute_id', 'filepath' ),
+            array( 'filepath' => $filePath ),
+            null,
+            null,
+            false
+        );
+    }
+
     static function moveFilepath( $contentObjectAttributeID, $oldFilepath, $newFilepath )
     {
         $db = eZDB::instance();
@@ -195,6 +214,69 @@
         eZPersistentObject::removeObject( eZImageFile::definition(), array( 'contentobject_attribute_id' => $contentObjectAttributeID ) );
     }
 
+    /**
+     * Tests if $filepath is referenced by content attributes of the same $attributeId in different $attributeVersion or $languageCode
+     *
+     * @param string $filepath
+     * @param int $attributeId Content object attribute ID
+     * @param int $attributeVersion Content object attribute version
+     * @param string $languageCode Attribute language code
+     * @return bool true if $filepath is referenced by other attributes, false otherwise
+     */
+    public static function isReferencedByOtherAttributes( $filepath, $attributeId, $attributeVersion, $languageCode )
+    {
+        $db = eZDB::instance();
+        $filepath = $db->escapeString( $filepath );
+
+        // Check eZImageFile and eZContentobjectAttribute for references to the alias file, in another version/language
+        $rows = $db->arrayQuery(
+            sprintf(
+                "
+                SELECT count(*) as count FROM ezcontentobject_attribute
+                INNER JOIN ezimagefile ON ezcontentobject_attribute.id=ezimagefile.contentobject_attribute_id
+                WHERE ezimagefile.filepath='%s'
+                AND ezcontentobject_attribute.data_text LIKE '%%url=\"%s\"%%'
+                AND ezcontentobject_attribute.id = %d
+                AND ( ezcontentobject_attribute.version != %d OR ezcontentobject_attribute.language_code != '%s' )
+                ",
+                $filepath, $filepath,
+                $attributeId,
+                $attributeVersion,
+                $languageCode
+            )
+        );
+
+        return (int)$rows[0]['count'] > 0;
+    }
+
+    /**
+     * Tests if there are ezimagefile rows with a different $attributeId that reference $filepath
+     *
+     * @param string $filepath
+     * @param int $attributeId
+     * @return bool
+     */
+    public static function isReferencedByOtherImageFiles( $filepath, $attributeId )
+    {
+        $db = eZDB::instance();
+        $filepath = $db->escapeString( $filepath );
+
+        $rows = $db->arrayQuery(
+            sprintf(
+                "
+                SELECT count(*) as count FROM ezcontentobject_attribute
+                INNER JOIN ezimagefile ON ezcontentobject_attribute.id=ezimagefile.contentobject_attribute_id
+                WHERE ezimagefile.filepath='%s'
+                AND ezcontentobject_attribute.id != %d
+                ",
+                $filepath,
+                $attributeId
+            )
+        );
+
+        return (int)$rows[0]['count'] > 0;
+    }
+
 
     /// \privatesection
     public $ID;
Index: kernel/classes/datatypes/ezimage/ezimagetype.php
===================================================================
--- kernel/classes/datatypes/ezimage/ezimagetype.php	(revision 70252)
+++ kernel/classes/datatypes/ezimage/ezimagetype.php	(working copy)
@@ -304,11 +304,9 @@
 
         }
 
-        if ( $content )
+        if ( $result && $hasImageAltText )
         {
-            if ( $hasImageAltText )
-                $content->setAttribute( 'alternative_text', $imageAltText );
-            $result = true;
+            $content->setAttribute( 'alternative_text', $imageAltText );
         }
 
         return $result;
