diff --git a/bin/indexer.php b/bin/indexer.php
index f6aeb4f0e3293eee3409d55a93fbd815ca365c3d..78f470ae0cb652bdf90554ede51f0df81b802a1e 100755
--- a/bin/indexer.php
+++ b/bin/indexer.php
@@ -91,63 +91,13 @@ function _index($id){
     _quietecho("done.\n");
 }
 
-/**
- * lock the indexer system
- */
-function _lock(){
-    global $conf;
-    $lock = $conf['lockdir'].'/_indexer.lock';
-    $said = false;
-    while(!@mkdir($lock, $conf['dmode'])){
-        if(time()-@filemtime($lock) > 60*5){
-            // looks like a stale lock - remove it
-            @rmdir($lock);
-        }else{
-            if($said){
-                _quietecho(".");
-            }else{
-                _quietecho("Waiting for lockfile (max. 5 min)");
-                $said = true;
-            }
-            sleep(15);
-        }
-    }
-    if($conf['dperm']) chmod($lock, $conf['dperm']);
-    if($said) _quietecho("\n");
-}
-
-/**
- * unlock the indexer sytem
- */
-function _unlock(){
-    global $conf;
-    $lock = $conf['lockdir'].'/_indexer.lock';
-    @rmdir($lock);
-}
-
 /**
  * Clear all index files
  */
 function _clearindex(){
-    global $conf;
-    _lock();
     _quietecho("Clearing index... ");
-    io_saveFile($conf['indexdir'].'/page.idx','');
-    io_saveFile($conf['indexdir'].'/title.idx','');
-    io_saveFile($conf['indexdir'].'/pageword.idx','');
-    io_saveFile($conf['indexdir'].'/metadata.idx','');
-    $dir = @opendir($conf['indexdir']);
-    if($dir!==false){
-        while(($f = readdir($dir)) !== false){
-            if(substr($f,-4)=='.idx' &&
-               (substr($f,0,1)=='i' || substr($f,0,1)=='w'
-               || substr($f,-6)=='_w.idx' || substr($f,-6)=='_i.idx' || substr($f,-6)=='_p.idx'))
-                @unlink($conf['indexdir']."/$f");
-        }
-    }
-    @unlink($conf['indexdir'].'/lengths.idx');
+    idx_get_indexer()->clear();
     _quietecho("done.\n");
-    _unlock();
 }
 
 function _quietecho($msg) {
diff --git a/inc/indexer.php b/inc/indexer.php
index 4dfaa33fb354bfa9afa189db8dd588e513c042ee..b9eaf31d9d9c30f74f7ff191f5973e1840f224e8 100644
--- a/inc/indexer.php
+++ b/inc/indexer.php
@@ -401,6 +401,38 @@ class Doku_Indexer {
         return true;
     }
 
+    /**
+     * Clear the whole index
+     *
+     * @return bool If the index has been cleared successfully
+     */
+    public function clear() {
+        global $conf;
+
+        if (!$this->lock()) return false;
+
+        @unlink($conf['indexdir'].'/page.idx');
+        @unlink($conf['indexdir'].'/title.idx');
+        @unlink($conf['indexdir'].'/pageword.idx');
+        @unlink($conf['indexdir'].'/metadata.idx');
+        $dir = @opendir($conf['indexdir']);
+        if($dir!==false){
+            while(($f = readdir($dir)) !== false){
+                if(substr($f,-4)=='.idx' &&
+                    (substr($f,0,1)=='i' || substr($f,0,1)=='w'
+                        || substr($f,-6)=='_w.idx' || substr($f,-6)=='_i.idx' || substr($f,-6)=='_p.idx'))
+                    @unlink($conf['indexdir']."/$f");
+            }
+        }
+        @unlink($conf['indexdir'].'/lengths.idx');
+
+        // clear the pid cache
+        $this->pidCache = array();
+
+        $this->unlock();
+        return true;
+    }
+
     /**
      * Split the text into words for fulltext search
      *