The reliable way is to generate a script from DBA_OBJECTS (or ALL_OBJECTS if you lack DBA rights), filter on STATUS = 'INVALID', and run each object's own DDL through DBMS_UTILITY.COMPILE_SCHEMA or a generated ALTER ... COMPILE statement—view first, then procedure/function/package/type, then triggers, and re-check what remains. Compilation doesn't fix invalid objects; it only re-validates them against current dependencies, so anything still invalid afterward points to a real error you need to read from *_ERRORS.
Before you start: privileges, versions, and what "invalid" means
You need the object type and owner in front of you before any script runs. STATUS = 'INVALID' lives in DBA_OBJECTS, ALL_OBJECTS, or USER_OBJECTS; the DBA view requires SELECT on it or the SELECT_CATALOG_ROLE, and compiling other schemas' objects requires ALTER ANY PROCEDURE, ALTER ANY TABLE, or ownership. If you only have ALL_OBJECTS, you'll see objects you can access, not every object in the database—so an "all invalid" count from that view is a different number than the DBA one, and both are correct within their scope.
Version matters in three ways. First, edition-based redefinition (11g onward) means a valid object in one edition can be invalid in another; DBA_OBJECTS reports the edition you're querying under. Second, Oracle 12.2 and later changed how many dependency states are tracked, so recompilation cascades can look shorter than on older releases. Third, from 12c onward the database runs automatic recompilation of dependent objects in the background after DDL, which occasionally makes a manual sweep appear to "do nothing"—the objects were already fixed. Check LAST_DDL_TIME before assuming your script failed.
One more definitional point: an invalid object is not necessarily broken. Oracle marks a dependent object invalid when a referenced object changes, then tries to re-validate it on first use (or immediately, if metadata dependencies allow). A package body invalidated by a table alteration usually recompiles cleanly the moment it's touched. A procedure whose SQL references a dropped column stays invalid until someone edits the code. Your sweep is really a triage: separate the "needs a nudge" objects from the "needs a developer" objects.
Step 1: Inventory the invalid objects with a query you can trust
Start with counts by owner and type so you know the size of the problem before you touch anything:
Then list the details, including when each object was last changed, so you can spot objects that went invalid long ago versus ones your own DDL just broke:
Expect the list to include synonyms, views, and public synonyms that point at invalid bases—these usually clear themselves once the base compiles. Expect it to include objects owned by SYS, SYSTEM, MDSYS, CTXSYS, and other Oracle-maintained schemas; those are normally left alone, and Oracle's own components can show transient invalids after patches or upgrades. Note the schema owners you actually care about before proceeding.
A useful check separates "invalid because a parent is invalid" from "invalid because of its own code." If you recompile one object and the invalids in the same dependency chain drop, that was cascade invalidation. If the count doesn't move, the object has a real error.
Step 2: Compile one schema at a time with DBMS_UTILITY.COMPILE_SCHEMA
DBMS_UTILITY.COMPILE_SCHEMA is the standard bulk tool. It compiles every invalid object in a schema, in dependency order where Oracle can determine it, and the second parameter controls whether it retries in a loop:
The parameters that matter: schema is the owner name in uppercase; compile_all => FALSE compiles only invalid objects (setting it TRUE recompiles everything, which is slower and usually unnecessary); reuse_settings => TRUE preserves existing session settings stored with the object rather than resetting them. On 10g and later you can also pass a PLSQL_CCFLAGS or conditional-compilation setting, though most sweeps don't need it.
Run it per schema in a loop rather than one giant call if you have many owners, so a failure in one schema doesn't obscure the others:
After it finishes, re-run the Step 1 count query. Objects that vanished from the list were cascade invalids; objects still listed need Step 3 or Step 5.
One report is important: COMPILE_SCHEMA doesn't always surface errors. If a schema's count stays high, don't conclude the procedure failed—it may have compiled successfully but the objects genuinely fail validation. Pull the errors next.
Step 3: Understand compilation order (and why packages come last)
Oracle resolves dependencies in a fixed hierarchy: tables and types are at the base, views sit on top of those, then procedures and functions, then packages (spec before body), then triggers, then synonyms. COMPILE_SCHEMA respects this where it can, but there's a well-known ordering wrinkle: package specs must compile before any object that references their public interface, and package bodies can fail if the spec is invalid. The sequence that avoids most cascades is:
1: Types and type bodies. 2: Tables and table constraints (rarely invalid, but altering them triggers cascades). 3: Views, materialized views, and synonyms. 4: Standalone procedures and functions. 5: Package specifications. 6: Package bodies. 7: Triggers. 8: Java sources and classes, if present.
You don't need to script that order by hand for a normal COMPILE_SCHEMA run—Oracle handles it—but you do need it when you fall back to individual ALTER statements, because compiling a package body before its spec wastes a pass.
There's a subtlety with ALTER ... COMPILE on a package: ALTER PACKAGE pkg COMPILE compiles the spec and body together and is what you normally want. ALTER PACKAGE pkg COMPILE BODY only touches the body and fails if the spec is invalid. If in doubt, use the version without BODY.
Step 4: Fall back to explicit ALTER statements when bulk compilation stalls
Some objects don't get picked up by COMPILE_SCHEMA—most often because they fail hard rather than compile, or because the caller lacks privilege for a specific type. Generate the statements rather than typing them:
Review the output, spool it to a file, and run it in a SQL*Plus or SQLcl session. Two failure modes are worth naming. A synonym whose target doesn't exist will never compile—you have to fix or drop the synonym, not recompile it. A view that references a dropped column compiles to an error whose message names the column; that's a code change, not a compile-retry situation. If you get a ORA-04045 or ORA-04063 chain about dependencies, the object referenced at the root of the message is your real fix.
The success check here is mechanical: run the Step 1 group-by query again. A lower count with the same owners and types means progress; an unchanged count means those objects have real errors.
Step 5: Diagnose what's still invalid by reading the errors
Every object still invalid has an entry in DBA_ERRORS (or ALL_ERRORS/USER_ERRORS). Get a compact readout:
The text column is the ORA message plus the compiler's explanation, and line points at the offending line in the stored source. Common causes and what to do:
ORA-00904: invalid identifier. A column referenced in the code was renamed or dropped. Fix the SQL in the object, not the compile. ORA-04063 / ORA-04068: existing state of packages has been discarded. Usually transient after a recompile; the next session that touches the package gets a fresh state. If the error persists, the package body genuinely fails on its own code. ORA-00942: table or view does not exist. Often a missing synonym or grant, or a table that was moved. Check whether the object is owned by a different schema and whether the compiling user has SELECT on it. PLS-00302: component must be declared. The referenced procedure or variable doesn't exist—usually a stale reference after an interface change.
One discipline matters here: don't blunt-fix errors by granting CREATE ANY privileges or globally disabling validation. Read each error against the source (DBA_SOURCE, or USER_SOURCE for your own objects), correct the actual code, and recompile that one object to confirm the error clears. The line number is the most useful field—match it to the source text before assuming the message means what you think.
Step 6: Run a full validity sweep and capture the before/after numbers
For a scheduled maintenance task, the useful pattern is a small script that snapshots invalids, compiles, and reports the delta. Here's a compact PL/SQL block that does all three without writing a file:
Schedule it as a DBMS_SCHEDULER job after deployments rather than manually. The check that the job actually worked isn't "the block ran without exception"—it's the reported delta plus a re-read of DBA_OBJECTS. A sweep that reports zero resolved and a large remaining count is a signal to stop automating and start reading DBA_ERRORS.
Command-line and third-party options worth knowing
SQL*Plus can drive a generated script from a file—spool the Step 4 ALTER statements, then @compile_invalids.sql. That's fine for a controlled window but has no dependency ordering, so expect two or three passes: order the statements by object type in the file (views first, then procedures, then packages), and repeat until the output stops producing new errors.
Oracle's own utlrp.sql script (?/rdbms/admin/utlrp.sql) recompiles invalid objects database-wide and is the standard tool after patching or upgrades. It's a wrapper around DBMS_UTILITY-style logic and is safe to run, but it deliberately skips some Oracle-maintained schemas and takes longer on large systems. Use it when your target is the whole database, and use COMPILE_SCHEMA per application schema when your target is a deployment.
Third-party tools (TOAD, SQL Developer's own recompile action, PL/SQL Developer) do the same thing—generate and execute ALTER statements behind a button. The button is convenient; the underlying list and error readout are identical. I'd rather run the queries yourself and understand the count than trust a green checkmark, because a "success" dialog on a still-invalid object is the most common way people lose an afternoon.
Why a second run sometimes matters, and what to do about it
Gen 2 errors are the annoyance. Suppose you fix a view that was blocking a procedure; the procedure may remain invalid until it's compiled after the view is valid in the same session sequence. Running the per-schema compile twice, or looping until the invalid count stops decreasing, catches most of these:
Looping is safe because compilation is idempotent—recompiling a valid object is a no-op. Stop after no change in the count between passes; further passes won't help and each one costs recompilation time on large schemas. If the count is stuck at a handful of objects across every pass, those are the ones the next section addresses.
I'd also watch for objects that alternate between valid and invalid across passes. Those usually sit in a circular dependency: package A calls package B, and B calls A, with neither spec compiled. The fix is to compile both specs first (ALTER PACKAGE ... COMPILE SPECIFICATION), then the bodies. That's an edge case, but it explains why some teams see a stubborn count that resets after each run.
When to stop and hand it to a developer or DBA
Recompilation is a maintenance action, not a repair. The stop condition is straightforward: if an object remains invalid after one COMPILE_SCHEMA pass and one explicit ALTER ... COMPILE pass, and DBA_ERRORS names a real code defect (missing column, missing object, undeclared component), further compiling will just repeat the same error. At that point you need the object's source and a code change, which is a developer task—not something to circumvent by dropping and recreating the object, especially if it's a package with dependents.
The other stop condition is scope. If you see invalid objects in Oracle-maintained schemas (SYS, MDSYS, CTXSYS, OLAPSYS, and similar), don't compile them individually. They can reflect an incomplete patch, an upgrade in progress, or a genuinely damaged component, and the supported path is to re-run the relevant patch or utlrp.sql under Oracle support guidance. Likewise, if DBA_OBJECTS shows invalid objects but DBA_ERRORS has no error text for them, that often means the object is invalid in an edition or container context you're not connected to—check CDB_OBJECTS if you're on a multitenant database, and check which PDB your session is in before drawing conclusions.
Finally, if compiles are failing with internal errors (ORA-00600 or ORA-07445) rather than ordinary compile errors, stop sweeping and open a service request with the trace file. Repeatedly recompiling against an internal error can multiply the trace files without resolving anything, and the fix belongs with Oracle support.
For everyday deployments, the whole routine is short: snapshot the invalid list, run DBMS_UTILITY.COMPILE_SCHEMA per application schema, re-run the generated ALTER statements once, read DBA_ERRORS for whatever's left, and treat the remaining set as a work queue for whoever owns the code. Doing it in that order keeps you from chasing cascade invalids that fix themselves and from mistaking a real bug for a compilation problem.