Library Version 12.1.6.0

Deleting Secondary Database Records
Prev | Chapter 10. Secondary Databases | Next

## Deleting Secondary Database Records

In general, you will not modify a secondary database directly. In order to modify a secondary database, you should modify the primary database and simply allow DB to manage the secondary modifications for you.

However, as a convenience, you can delete `SecondaryDatabase` records directly. Doing so causes the associated primary key/data pair to be deleted. This in turn causes DB to delete all `SecondaryDatabase` records that reference the primary record.

You can use the `SecondaryDatabase.delete()` method to delete a secondary database record. Note that if your `SecondaryDatabase` contains duplicate records, then deleting a record from the set of duplicates causes all of the duplicates to be deleted as well.

### Note

`SecondaryDatabase.delete()` causes the previously described delete operations to occur only if the primary database is opened for write access.

For example:

    package db.GettingStarted;

    import com.sleepycat.db.DatabaseEntry;
    import com.sleepycat.db.DatabaseException;
    import com.sleepycat.db.OperationStatus;
    import com.sleepycat.db.SecondaryDatabase;

    ...
    try {
        SecondaryDatabase mySecondaryDatabase = null;
        // Omitting all database opens
        ...

        String searchName = "John Doe";
        DatabaseEntry searchKey =
            new DatabaseEntry(searchName.getBytes("UTF-8"));

        // Delete the first secondary record that uses "John Doe" as
        // a key. This causes the primary record referenced by this secondary
        // record to be deleted.
        OperationStatus retVal = mySecondaryDatabase.delete(null, searchKey);
    } catch (Exception e) {
        // Exception handling goes here
    }

Prev | Up | Next
Reading Secondary Databases | Home | Using Secondary Cursors