Deleting Secondary Database Records
Prev | Chapter 5. 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 a `SecondaryDatabase` record 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 describe delete operations to occur only if:

- the `SecondaryKeyCreator.createSecondaryKey()` method returns `true` (see Implementing Key Creators for information on this interface and method).

- the primary database is opened for write access.

If either of these conditions are not met, then no delete operations can be performed on the secondary database.

For example:

    package com.sleepycat.examples.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