Index: src/edu/ucsb/nceas/metacat/IndexingQueue.java
===================================================================
--- src/edu/ucsb/nceas/metacat/IndexingQueue.java	(revision 7432)
+++ src/edu/ucsb/nceas/metacat/IndexingQueue.java	(revision 7433)
@@ -120,7 +120,7 @@
             try {
             	indexingMap.wait();
             } catch (InterruptedException ex) {
-              System.err.println("Interrupted");
+              logMetacat.error("Interrupted");
             }
           }
           
@@ -132,6 +132,23 @@
         }
         return returnVal;
       }
+    
+    /**
+     * Removes the Indexing Task object from the queue
+     * for the given docid. Currently, rev is ignored
+     * This method should be used to cancel scheduled indexing on a document
+     * (typically if it is being deleted but indexing has not completed yet)
+     * see http://bugzilla.ecoinformatics.org/show_bug.cgi?id=5750
+     * @param docid the docid (without revision)
+     * @param rev the docid's rev (ignored)
+     */
+    public void remove(String docid, String rev) {
+		synchronized (indexingMap) {
+			if (indexingMap.containsKey(docid)) {
+				indexingMap.remove(docid);
+			}
+		}
+	}
 
 }
 
@@ -157,16 +174,16 @@
 	  public void run() {
 	    while (metacatRunning) {
 	      // blocks until job
-	      IndexingQueueObject returnVal = 
+	      IndexingQueueObject indexQueueObject = 
 	    	  IndexingQueue.getInstance().getNext();
 
-	      if(returnVal != null){
+	      if(indexQueueObject != null){
 	    	  if(!IndexingQueue.getInstance().
-	    			currentDocidsBeingIndexed.contains(returnVal.getDocid())){
+	    			currentDocidsBeingIndexed.contains(indexQueueObject.getDocid())){
     		  try {
     			  IndexingQueue.getInstance().
-    			  		currentDocidsBeingIndexed.add(returnVal.getDocid());
-    		      String docid = returnVal.getDocid() + "." + returnVal.getRev();
+    			  		currentDocidsBeingIndexed.add(indexQueueObject.getDocid());
+    		      String docid = indexQueueObject.getDocid() + "." + indexQueueObject.getRev();
     			  if(checkDocumentTable(docid, "xml_documents")){
     				  logMetacat.warn("Calling buildIndex for " + docid);
     				  DocumentImpl doc = new DocumentImpl(docid, false);
@@ -183,12 +200,12 @@
     			  logMetacat.warn("Exception: " + e);
     			  e.printStackTrace();
 	        
-    			  if(returnVal.getCount() < 25){
-    				  returnVal.setCount(returnVal.getCount()+1);
+    			  if(indexQueueObject.getCount() < 25){
+    				  indexQueueObject.setCount(indexQueueObject.getCount()+1);
     				  // add the docid back to the list
-    				  IndexingQueue.getInstance().add(returnVal);
+    				  IndexingQueue.getInstance().add(indexQueueObject);
     			  } else {
-    				  logMetacat.fatal("Docid " + returnVal.getDocid() 
+    				  logMetacat.fatal("Docid " + indexQueueObject.getDocid() 
 	        			+ " has been inserted to IndexingQueue "
 	        			+ "more than 25 times. Not adding the docid to"
 	        			+ " the queue again.");
@@ -195,11 +212,11 @@
     			  }
     		  	} finally {
     			  	IndexingQueue.getInstance().currentDocidsBeingIndexed
-    			  		.remove(returnVal.getDocid());	    	  
+    			  		.remove(indexQueueObject.getDocid());	    	  
     		  	}
 	    	  } else {
-	    		  returnVal.setCount(returnVal.getCount()+1);
-	    		  IndexingQueue.getInstance().add(returnVal);    		  
+	    		  indexQueueObject.setCount(indexQueueObject.getCount()+1);
+	    		  IndexingQueue.getInstance().add(indexQueueObject);    		  
 	    	  }
 	      }
 	    }
Index: src/edu/ucsb/nceas/metacat/DocumentImpl.java
===================================================================
--- src/edu/ucsb/nceas/metacat/DocumentImpl.java	(revision 7432)
+++ src/edu/ucsb/nceas/metacat/DocumentImpl.java	(revision 7433)
@@ -2920,6 +2920,14 @@
             	IndexingQueue.getInstance().add(docid, rev);
         }
     }
+    
+    private static void removeDocidFromIndexingQueue(String docid, String rev) throws PropertyNotFoundException {
+        boolean useXMLIndex =
+            (new Boolean(PropertyService.getProperty("database.usexmlindex"))).booleanValue();
+        if (useXMLIndex) {
+            	IndexingQueue.getInstance().remove(docid, rev);
+        }
+    }
 
     /**
      * Write an XML file to the database during replication
@@ -3309,6 +3317,9 @@
             double afterArchiveDocAndNode = System.currentTimeMillis()/1000;
             logMetacat.info("DocumentImpl.delete - The time for archiveDocAndNodesRevision is "+(afterArchiveDocAndNode - start));
             
+            // make sure we don't have a pending index task
+            removeDocidFromIndexingQueue(docid, String.valueOf(rev));
+            
             // Now delete it from the xml_index table
             pstmt = conn.prepareStatement("DELETE FROM xml_index WHERE docid = ?");
             pstmt.setString(1, docid);
