diff --git a/conf/dokuwiki.php b/conf/dokuwiki.php
index 67b2d9936dde24b5aa99b4c0559cb425a7c0793f..bd4eccfb5c64a7cc08e87761f44059b8517b1842 100644
--- a/conf/dokuwiki.php
+++ b/conf/dokuwiki.php
@@ -36,6 +36,8 @@ $conf['maxseclevel'] = 3;                 //Up to which level create editable se
 $conf['camelcase']   = 0;                 //Use CamelCase for linking? (I don't like it) 0|1
 $conf['deaccent']    = 1;                 //convert accented chars to unaccented ones in pagenames?
 $conf['useheading']  = 0;                 //use the first heading in a page as its name
+$conf['refcheck']    = 1;                 //check references before deleting media files
+$conf['refcount']    = 5;                 //search only no of references to satisfy the refcheck
 
 /* Antispam Features */
 
diff --git a/inc/lang/de/lang.php b/inc/lang/de/lang.php
index 236a36aee41a85032816c1a0eb5d480a4063719f..7e3e46469d0c0dddd9be0951cd61c27e12eb4eab 100644
--- a/inc/lang/de/lang.php
+++ b/inc/lang/de/lang.php
@@ -71,6 +71,9 @@ $lang['uploadsucc']  = 'Datei wurde erfolgreich hochgeladen';
 $lang['uploadfail']  = 'Hochladen fehlgeschlagen. Keine Berechtigung?';
 $lang['uploadwrong'] = 'Hochladen verweigert. Diese Dateiendung ist nicht erlaubt.';
 $lang['uploadexist'] = 'Datei existiert bereits. Keine Änderungen vorgenommen.';
+$lang['deletesucc']  = 'Die Datei "%s" wurde gelöscht.';
+$lang['deletefail']  = '"%s" konnte nicht gelöscht werden - prüfen Sie die Berechtigungen.';
+$lang['mediainuse']  = 'Die Datei "%s" wurde nicht gelöscht - sie wird noch verwendet.';
 $lang['namespaces']  = 'Namensräume';
 $lang['mediafiles']  = 'Vorhandene Dateien in';
 
diff --git a/inc/lang/en/lang.php b/inc/lang/en/lang.php
index 9dd2f46812946281b8a18ccf09b2bac0c2e3d974..235eeff80a1db452a43a49f5974abec40c696e32 100644
--- a/inc/lang/en/lang.php
+++ b/inc/lang/en/lang.php
@@ -69,6 +69,9 @@ $lang['uploadsucc']  = 'Upload successful';
 $lang['uploadfail']  = 'Upload failed. Maybe wrong permissions?';
 $lang['uploadwrong'] = 'Upload denied. This file extension is forbidden!';
 $lang['uploadexist'] = 'File already exists. Nothing done.';
+$lang['deletesucc']  = 'The file "%s" has been deleted.';
+$lang['deletefail']  = '"%s" couldn\'t be deleted - check perission.';
+$lang['mediainuse']  = 'The file "%s" hasn\'t been deleted - it is still in use.';
 $lang['namespaces']  = 'Namespaces';
 $lang['mediafiles']  = 'Available files in';
 
diff --git a/inc/search.php b/inc/search.php
index 4b1973e989dc59f59948a9b7ca707c8e3c68ce01..0caae3d7c602e20fab7e2cb44cce130b67db6d02 100644
--- a/inc/search.php
+++ b/inc/search.php
@@ -294,11 +294,6 @@ function search_fulltext(&$data,$base,$file,$type,$lvl,$opts){
     return false;
   }
 
-  //get text
-  $text = io_readfile($base.'/'.$file);
-  //lowercase text (u modifier does not help with case)
-  $lctext = utf8_strtolower($text);
-
   //create regexp from queries  
   $poswords = array();
   $negwords = array();
@@ -323,12 +318,77 @@ function search_fulltext(&$data,$base,$file,$type,$lvl,$opts){
   
   $reg  = '^(?=.*?'.join(')(?=.*?',$poswords).')';
   $reg .= count($negwords) ? '((?!'.join('|',$negwords).').)*$' : '.*$';
+  search_regex($data,$base,$file,$reg,$poswords);
+  return true;
+}
+
+/**
+ * Reference search
+ * This fuction searches for existing references to a given media file
+ * and returns an array with the found pages. It doesn't pay any
+ * attention to ACL permissions to find every reference. The caller
+ * must check if the user has the appropriate rights to see the found
+ * page and eventually have to prevent the result from displaying.
+ *
+ * @param array  $data Reference to the result data structure
+ * @param string $base Base usually $conf['datadir']
+ * @param string $file current file or directory relative to $base
+ * @param char   $type Type either 'd' for directory or 'f' for file
+ * @param int    $lvl  Current recursion depht
+ * @param mixed  $opts option array as given to search()
+ *
+ * $opts['query'] is the demanded media file name
+ *
+ * @author  Andreas Gohr <andi@splitbrain.org>
+ * @author  Matthias Grimm <matthiasgrimm@users.sourceforge.net>
+ */
+function search_reference(&$data,$base,$file,$type,$lvl,$opts){
+  global $conf;
+  
+  //we do nothing with directories
+  if($type == 'd') return true;
+  
+  //only search txt files
+  if(!preg_match('#\.txt$#',$file)) return true;
+
+  //we finish after five references found. The return value
+  //'false' will skip subdirectories to speed search up.
+  if(count($data) >= $conf['refcount']) return false;
   
+  $reg = '{{ *'.$opts['query'].' *(\|.*)?}}';
+  search_regex($data,$base,$file,$reg,array($opts['query']));
+  return true;
+}
+
+/* ------------- helper functions below -------------- */
+
+/**
+ * fulltext search helper
+ * searches a text file with a given regular expression
+ * no ACL checks are performed. This have to be done by
+ * the caller if necessary.
+ *
+ * @param array  $data  reference to array for results
+ * @param string $base  base directory
+ * @param string $file  file name to search in
+ * @param string $reg   regular expression to search for
+ * @param array  $words words that should be marked in the results
+ *
+ * @author  Andreas Gohr <andi@splitbrain.org>
+ * @author  Matthias Grimm <matthiasgrimm@users.sourceforge.net>
+ */
+function search_regex(&$data,$base,$file,$reg,$words){
+
+  //get text
+  $text = io_readfile($base.'/'.$file);
+  //lowercase text (u modifier does not help with case)
+  $lctext = utf8_strtolower($text);
+
   //do the fulltext search
   $matches = array();
   if($cnt = preg_match_all('#'.$reg.'#usi',$lctext,$matches)){
     //this is not the best way for snippet generation but the fastest I could find
-    $q = $poswords[0];  //use first posword for snippet
+    $q = $words[0];  //use first word for snippet creation
     $p = utf8_strpos($lctext,$q);
     $f = $p - 100;
     $l = utf8_strlen($q) + 200;
@@ -336,20 +396,21 @@ function search_fulltext(&$data,$base,$file,$type,$lvl,$opts){
     $snippet = '<span class="search_sep"> ... </span>'.
                htmlspecialchars(utf8_substr($text,$f,$l)).
                '<span class="search_sep"> ... </span>';
-    $mark    = '('.join('|',$poswords).')';
+    $mark    = '('.join('|', $words).')';
     $snippet = preg_replace('#'.$mark.'#si','<span class="search_hit">\\1</span>',$snippet);
 
     $data[] = array(
-      'id'       => $id,
+      'id'       => pathID($file),
       'count'    => preg_match_all('#'.$mark.'#usi',$lctext,$matches),
-      'poswords' => join(' ',$poswords),
+      'poswords' => join(' ',$words),
       'snippet'  => $snippet,
     );
   }
-  
+ 
   return true;
 }
 
+
 /**
  * fulltext sort
  *
diff --git a/lib/exe/media.php b/lib/exe/media.php
index 4e193807ccfca1b96785936f61ee3444a6d674a1..f0d0795adee53fe9885e23ff656846f8ee813c29 100644
--- a/lib/exe/media.php
+++ b/lib/exe/media.php
@@ -34,11 +34,20 @@
   }
 
   //handle deletion
-	if($DEL && $AUTH >= AUTH_DELETE){
-		media_delete($DEL);
+  $mediareferences = array();
+  if($DEL && $AUTH >= AUTH_DELETE){
+	if($conf['refcheck']){
+  	  search($mediareferences,$conf['datadir'],'search_reference',array('query' => $DEL));
 	}
+    if(!count($mediareferences)){
+      media_delete($DEL);
+    }else{
+      $text = str_replace('%s',noNS($DEL),$lang['mediainuse']);
+      msg($text,0);
+    }
+  }
 
-	//handle upload
+  //handle upload
   if($_FILES['upload']['tmp_name'] && $UPLOADOK){
     media_upload($NS,$AUTH);
   }
@@ -58,12 +67,16 @@
  * @author Andreas Gohr <andi@splitbrain.org>
  */
 function media_delete($delid){
+  global $lang;
+
   $file = mediaFN($delid);
-	if(@unlink($file)){
-		return true;
-	}
-	//something went wrong
-  msg("'$file' couldn't be deleted - check permissions",-1);
+  if(@unlink($file)){
+    msg(str_replace('%s',noNS($delid),$lang['deletesucc']),1);
+    return true;
+  }
+  //something went wrong
+  $text = str_replace('%s',$file,$lang['deletefail']);
+  msg($text,-1);
   return false;
 }