Index: media.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/media/media.module,v
retrieving revision 1.48
diff -u -p -r1.48 media.module
--- media.module	14 Sep 2010 18:25:39 -0000	1.48
+++ media.module	15 Sep 2010 04:51:33 -0000
@@ -378,31 +378,6 @@ function media_image_default_styles() {
 }
 
 /**
- * Implement hook_file_load().
- */
-function media_file_load($files) {
-  foreach ($files as $file) {
-    // The media module maintains "media" entities whose entity id correspond to
-    // the file's fid. These entities have a "file" field, which like all file
-    // fields, references the file's fid. This creates a self-referential
-    // problem, where each media file always has one field item referencing it
-    // (the one by its own media entity), making file_delete() always fail,
-    // unless we correct it. So, we correct it by telling
-    // file_get_file_reference_count() to ignore these self-references.
-    // @todo Since these are scalar variables rather than arrays, they can be
-    //   clobbered by other modules implementing their own reference ignoring
-    //   logic (for example, file.module itself sets these variables under
-    //   certain conditions). When these variables are clobbered, the
-    //   self-reference problem resurfaces and prevents file deletion. We should
-    //   submit a core patch for file_get_file_reference_count() to support more
-    //   flexibility and module interoperability in this regard.
-    $file->file_field_name = 'file';
-    $file->file_field_type = 'media';
-    $file->file_field_id = $file->fid;
-  }
-}
-
-/**
  * Implement hook_file_insert().
  */
 function media_file_insert($file) {
@@ -589,6 +564,18 @@ function media_save($media) {
   else {
     field_attach_update('media', $media);
   }
+
+  // Logically, the media module attempts to make files into entities (i.e.,
+  // media "IS A" file), but implementationally, media entities contain a file
+  // field (i.e., media "HAS A" file). This "HAS A" implementation causes
+  // file.module to create undesired self references in the {file_usage} table,
+  // which incorrectly makes all files "in use" and therefore, undeletable. We
+  // remove those self-reference artifacts of the "HAS A" implementation here.
+  if ($media->fid) {
+    // file_usage_delete() requires an instance of stdClass, whereas $media is
+    // an instance of some other class, so we need to cast it to stdClass.
+    file_usage_delete((object)(array) $media, 'file', 'media', $media->fid);
+  }
 }
 
 /**
@@ -913,7 +900,10 @@ function media_save_attached_file($file)
       field_attach_presave('media', $media);
       field_attach_update('media', $media);
       //field_attach_insert('media', $media);
-
+      // @todo Why aren't we calling media_save() instead of the above code.
+      //   Since we aren't, we have to update usage here too, as we do in
+      //   media_save().
+      file_usage_delete((object)(array) $media, 'file', 'media', $media->fid);
     }
   }
 
