Deleting Records Using Cursors

To delete a record using a cursor, simply position the cursor to the record that you want to delete and then call Cursor.delete(). Note that after deleting a record, the value of Cursor.getCurrent() is unchanged until such a time as the cursor is moved again. Also, if you call Cursor.delete() two or more times in a row without repositioning the cursor, then all subsequent deletes result in a return value of OperationStatus.KEYEMPTY.

For example:

import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.Database;
import com.sleepycat.je.Cursor;
import com.sleepycat.je.OperationStatus; 
import com.sleepycat.je.LockMode;

...
    
try {
    // Create DatabaseEntry objects
    // searchKey is some String.
    DatabaseEntry theKey = new DatabaseEntry(searchKey.getBytes("UTF-8"));
    DatabaseEntry theData = new DatabaseEntry();

    // Open a cursor using a database handle
    Cursor cursor = myDatabase.openCursor(null, null);

    // Position the cursor
    // Ignoring the return value for clarity
    OperationStatus retVal = cursor.getSearchKey(theKey, theData, 
                                                 LockMode.DEFAULT);
    
    // Count the number of records using the given key. If there is only
    // one, delete that record.
    if (cursor.count() == 1) {
            System.out.println("Deleting " + 
                               new String(theKey.getData() + "|" +
                               new String(theData.getData());
            cursor.delete();
    }
} catch (Exception e) {
    // Exception handling goes here
}