You make a package invalid in Oracle by changing what the package depends on or by forcing a recompile, then checking the data dictionary. The most controlled method is issuing ALTER PACKAGE ... COMPILE after an edit to the specification or body, since a failed compilation leaves the package in an invalid state; the safest method is to let dependency invalidation happen naturally instead of planting a permanent failure.
That distinction matters more than it first appears. In Oracle Database, "invalid" is a status flag in the data dictionary, not a corruption and not necessarily a broken object. An invalid package can often still be executed, because Oracle recompiles it on first use if it can. So the real question behind this task is usually one of three things: you want to test how your application behaves when recompilation fails, you want to force dependent objects to be re-resolved, or you want to verify that a deployment script recompiles everything it should. Each goal calls for a different technique.
This article covers the mechanics of package state, ordered procedures for each legitimate method, how to verify the result, how to reverse it, and where the approach breaks down. Commands reflect behavior documented for Oracle Database 11g through 23ai; the dictionary views and ALTER PACKAGE syntax have been stable across those releases, though editioning and PL/SQL-only compilation settings in 12c and later can change what you observe.
What "invalid" actually means for a package
Every PL/SQL unit gets a status in ALL_OBJECTS (or USER_OBJECTS): VALID or INVALID. For packages, the specification and body are separate objects with separate statuses. A package can have a valid spec and an invalid body, and that combination behaves differently from an invalid spec.
The status is a compilation flag, not a runtime contract. When a session references a package whose status is INVALID, the database attempts to recompile it before executing. If recompilation succeeds, the call proceeds and the status flips back to VALID. If it fails, the caller receives a compilation error at that moment. This lazy recompilation is why invalid objects frequently sit in a schema for months without visible harm: they are only recompiled when touched, and they were invalidated by a change that didn't actually break them.
Invalidation happens automatically when Oracle detects that a dependency changed. The classic mechanisms are:
A referenced table, view, or synonym is altered or dropped. A referenced package's specification is recompiled or replaced. A referenced standalone procedure or function is changed. A granted privilege used by the unit is revoked. Certain ALTER TABLE operations, such as adding a column to a table referenced by SELECT *, can invalidate dependents depending on version and settings.
The dependency is tracked at a granular level. A package body that calls only pkg_a.proc_one may be invalidated by a change to pkg_a's specification even if proc_one's signature is untouched. This conservative behavior is why large schemas accumulate invalid objects after each deployment and why "recompile everything" scripts exist.
The distinction between spec and body drives most of the practical consequences:
| Change | Spec status | Body status | Effect on callers |
|---|---|---|---|
| Body edited, spec untouched | VALID | INVALID | Callers keep working; body recompiles on next call |
| Spec edited (signature change) | INVALID | INVALID | Dependent units invalidated; calls may fail |
| Referenced table altered | VALID | INVALID | Body recompiles on next call if still valid |
| Compilation attempted and failed | INVALID (or unchanged) | INVALID | Calls fail until fixed |
The key insight: invalidating a body is low-risk and often self-healing, while invalidating a spec cascades to everything that references the package. If your goal is simply to observe invalidity, target the body with a failed compile rather than touching the specification.
Prerequisites and permissions
Before running any of the procedures below, confirm four things.
First, you need the appropriate privilege. Compiling your own objects requires no special grant. Compiling objects in another schema requires ALTER ANY PROCEDURE, or the ALTER object privilege on that specific package, granted directly or through a role.
Second, the object must be a stored package, not a package defined inside an anonymous block or a local subprogram. Local packages cannot be invalidated in the data dictionary because they don't persist there.
Third, the schema must have valid objects to reference. If you're deliberately breaking a compile, the failure must come from something you control, such as a missing table or a type mismatch, not from a schema-wide problem.
Fourth, establish a baseline. Run this query and save the output before you start, so you can return to the original state:
For another schema, substitute ALL_OBJECTS and add WHERE owner = 'SCHEMA_NAME'. Note that in ALL_OBJECTS the two rows are labeled PACKAGE and PACKAGE BODY, while USER_OBJECTS sometimes reports the body type as PACKAGE BODY and sometimes as PACKAGE depending on the view; filter carefully so you don't count the same package twice.
Method 1: Force a compile failure with a deliberate defect
This is the most direct way to leave a package invalid on purpose, useful for testing error handling or verifying that your deployment tooling reports failures correctly. It works because Oracle replaces the stored source only when compilation succeeds; a failed compile leaves the previous version in place but marks the object invalid.
1: Save the current source. Pull it from USER_SOURCE or your version control copy so you can restore it exactly:
2: Introduce a defect into the body only, never the specification. Typical choices: reference a table that doesn't exist, call a procedure with the wrong number of arguments, or use an undeclared variable. Keep the specification untouched.
3: Compile the body:
4: Expect ORA-24344: success with compilation error or a specific PLS- error, such as PLS-00201: identifier 'MISSING_TABLE' must be declared. This message is a success in the sense that the statement executed; the compilation itself failed.
5: Verify the status:
You should see the body as INVALID while the specification remains VALID. If both show INVALID, you edited the spec by mistake.
A simple success check: query USER_ERRORS for the package. If rows appear, the compile failed as intended and the object is invalid. If no rows appear and the status is VALID, the defect wasn't actually a defect, or you fixed it without noticing.
One caution: don't use CREATE OR REPLACE to inject the defect if you want the old body preserved. CREATE OR REPLACE with a defective body also fails and leaves the prior version intact, which is fine, but the ALTER PACKAGE ... COMPILE route makes the intent clearer in scripts and audit trails.
Method 2: Invalidate through dependency changes
If your goal is realistic invalidation rather than an artificial failure, change something the package depends on. This mimics what happens during a real schema migration.
1: Identify dependencies with USER_DEPENDENCIES or ALL_DEPENDENCIES:
2: Choose a dependency whose change you're willing to make. Altering a referenced table is the most common trigger. For example, renaming a column the package references will invalidate it; adding a column generally will not, though SELECT * usage can change that.
3: Perform the change, then check status. The package body should now be INVALID.
4: Test the self-healing behavior. Call a procedure in the package from a new session. If the dependency change didn't break the code, Oracle recompiles silently and the call succeeds. Check the status again: it should be VALID. If the change did break the code, the call raises a compilation error and the status stays INVALID.
This method teaches the most important lesson about invalidity: it is usually transient. Invalidation is Oracle's signal to re-resolve dependencies, not a verdict that the code is wrong.
A specific example makes the mechanism concrete. Suppose orders_pkg selects from orders. Renaming orders.status to orders.order_status invalidates the package body, because the parsed dependency references a column that no longer exists under that name. If the package body text still says status, the next call fails with ORA-00904: "STATUS": invalid identifier surfaced as a PL/SQL compilation error. If you also update the package body to use the new name and recompile, everything returns to VALID.
Method 3: Use DBMS_UTILITY to invalidate dependents
Oracle provides DBMS_UTILITY.INVALIDATE, which marks a named object and optionally its dependents as invalid without touching source code. This is the cleanest programmatic method and is documented for supported releases.
1: Confirm you have execute privilege on DBMS_UTILITY, which is granted to PUBLIC by default in most installations.
2: Run the procedure with the object's schema, name, and type:
The third parameter controls dependency handling: pass 1 to invalidate dependent objects as well, or 0 to invalidate only the named object. The package type argument is optional and defaults to PACKAGE_SPEC in some versions, so check the exact signature for your release before relying on defaults; passing the type explicitly as 'PACKAGE' or 'PACKAGE BODY' avoids ambiguity.
3: Verify status with the same USER_OBJECTS query.
4: Note the limitation: this marks objects invalid but does not insert rows into USER_ERRORS, because no compilation was attempted. If your verification process depends on USER_ERRORS, this method won't satisfy it.
This is the method to reach for in test harnesses and CI pipelines, because it's fast, reversible by a normal recompile, and doesn't require you to maintain deliberately broken source files.
Method 4: Edit the specification to cascade invalidation
Touching the specification is the heavyweight option. Any change to a package specification invalidates the specification itself and every unit that references it, including dependent packages, views, and triggers.
1: Add a harmless element to the spec, such as a new constant or a new procedure declaration.
2: Compile the specification:
3: Expect the spec and body to show INVALID, along with dependent objects. Query USER_OBJECTS filtered on status = 'INVALID' to see the blast radius.
4: Recompile the body, then the dependents, or run a schema-wide recompile (covered below).
I'd reserve this method for scenarios where you actually need cascading invalidation, because the recovery work grows with the number of dependents. In a schema with dozens of packages referencing a shared utility package, a spec change can invalidate hundreds of objects, and some of them may fail to recompile if their own dependencies drifted. That is a realistic outcome of a sloppy deployment, which is exactly why it's worth testing in a sandbox before it happens in production.
Verifying the invalid state
Checking status is straightforward; interpreting it is where people go wrong.
Three checks matter:
Count both rows. For each package, confirm whether spec, body, or both are invalid. An invalid body alone is usually benign; an invalid spec is not. Check USER_ERRORS. Rows here mean a compilation was attempted and failed. No rows with an INVALID status usually means the object was invalidated without a compile attempt, which is the normal aftereffect of a dependency change. Test the call path. From a separate session, invoke a procedure in the package. Success means the package self-healed; a compilation error means the defect is real. Doing this in the same session that compiled the object can be misleading, because session-level caching and the PLSQL_CCFLAGS and optimization settings can affect what you observe.
For a schema-wide view, join ALL_OBJECTS against ALL_ERRORS on owner and name to see which invalid objects also carry errors. That combination is the actionable set; invalid objects without errors typically just need a recompile to clear.
Reversing it: restoring valid state
Whatever method you used, recovery follows the same two steps.
1: Remove the cause. Restore the original package body from the source you saved, or revert the dependency change, or remove the spec edit.
2: Recompile:
The first statement compiles the specification and body together in most cases; the second targets the body explicitly. Running both in that order is harmless and covers version differences in how ALTER PACKAGE ... COMPILE behaves when the specification is already valid.
3: Recompile dependents if the spec was touched:
Setting compile_all to FALSE recompiles only invalid objects, which is faster and avoids touching valid code. If some objects remain invalid after this, query USER_ERRORS; the remaining failures are genuine code problems, not stale flags. This is your stop condition: when COMPILE_SCHEMA leaves objects invalid with errors you didn't introduce, hand the work to the package owner or DBA rather than editing code you don't understand.
4: Confirm with the baseline query you saved at the start. Statuses and the invalid-object count should match.
A practical note on recompilation cost: COMPILE_SCHEMA on a large schema can take minutes and holds library cache locks that briefly block other sessions from compiling the same objects. In a busy environment, schedule it in a maintenance window or target individual packages.
Limitations, version differences, and where this goes wrong
Several behaviors vary by release and configuration, and ignoring them produces confusing results.
Edition-based redefinition. In 11g and later, if editioning is enabled, an object's status observed from one edition can differ from another. Invalidation applies to the edition in which the change occurred. If you're testing in an environment with editions enabled, verify which edition your session sees with SELECT SYS_CONTEXT('USERENV', 'CURRENT_EDITION_NAME') FROM dual, and check status within that edition.
PL/SQL-only compilation. The PLSQL_CODE_TYPE and PLSQL_OPTIMIZE_LEVEL parameters, plus PLSQL_WARNINGS settings, can change whether a package compiles cleanly. A body that compiles under interpreted mode may behave differently under native compilation on some platforms. If an invalidation test passes in one environment and fails in another, compare these parameters before assuming a logic problem.
DBMS_UTILITY.INVALIDATE signature drift. The procedure's parameter list has changed across versions, with the object type parameter being optional in some releases. Relying on defaults is the most common source of ORA-06550 errors when running the one-liner from an older script. Passing all arguments explicitly is safer.
Privilege-based invalidation. Revoking a role or object privilege that a package relies on can invalidate it. This is a legitimate test in security reviews but a dangerous one in production, because recompilation may fail for every dependent unit at once and the failure only surfaces when someone calls the code.
The self-healing trap. Because invalid objects often recompile on use, a test that marks a package invalid and then immediately queries its status from the same session may show it as valid again. Use a fresh session to observe the intermediate state, or accept that the invalidation window is measured in the time between the compile statement and the next reference.
A genuine failure mode. A package body that references a table via SELECT * can break when a column is dropped even if the code doesn't use that column, because the stored parse includes the column list at compile time. Invalidation happens, the recompile fails with an error, and the fix requires editing the SELECT *. This is one of the few cases where an invalidation event surfaces a real latent bug, and it's a good reason to run invalidation tests as part of schema migration rehearsal rather than only when something breaks.
What the task is really for
Behind the mechanical question is a practical judgment call. Invalidating a package is easy; invalidating it in a way that teaches you something or tests the right failure path is the part that requires thought.
If you're writing a deployment script, the useful test is: after each migration step, which objects are invalid, and do they all recompile cleanly? That's a COMPILE_SCHEMA plus an error check, not a manual invalidation. If you're testing application error handling, the useful test is a body-only compile failure with a deliberate defect, which exercises the path where a caller gets a PL/SQL compilation error at runtime. If you're auditing dependencies, DBMS_UTILITY.INVALIDATE with the dependents flag shows you the blast radius cheaply.
I'd avoid the specification-edit method for routine testing; it produces the most realistic cascade but the most cleanup. And I'd resist the temptation to leave invalid objects in a shared development schema, since the next person to call the package gets a compilation error that has nothing to do with their work.
The one habit that prevents most trouble: always save the source and record the pre-test status before you invalidate anything. Restoring is trivial when you have the source; it becomes archaeology when you don't.