OP_DROP - Removing Top Stack Item
1. OP_DROP — Removing Top Stack Item
Overview
OP_DROP is one of the most fundamental stack manipulation opcodes in Bitcoin Script. Its sole purpose is to remove — or "drop" — the topmost item from the stack, discarding it entirely. While it may appear trivially simple, OP_DROP plays a surprisingly important role in Bitcoin script construction, especially in scenarios where intermediate computation results are no longer needed, or when a script pushes a value purely to satisfy conditional logic without needing to retain that value for later verification.
Opcode Reference
Opcode: OP_DROP
Hex: 0x75
Word: DROP
Input: x
Output: (nothing)
Stack Before: [ ..., x ]
Stack After: [ ... ]
How It Works
When the Bitcoin Script interpreter encounters OP_DROP, it pops the top element off the main stack and discards it. No checksum is computed, no hash is applied — the value is simply gone. This operation is O(1) in complexity, making it extremely efficient. If the stack is empty when OP_DROP is encountered, the script fails immediately with a stack underflow error.
Practical Example
Consider a simple script that pushes two values and then drops one:
Script: OP_1 OP_2 OP_DROP
Execution trace:
Initial Stack: []
Step 1 — OP_1:
Stack: [ 1 ]
Step 2 — OP_2:
Stack: [ 1, 2 ] ← (top is 2)
Step 3 — OP_DROP:
Stack: [ 1 ] ← (2 is discarded)
Real-World Usage in Bitcoin
One of the most common places you'll find OP_DROP in practice is in Hash Time-Locked Contracts (HTLCs) and time-locked scripts, where a CHECKLOCKTIMEVERIFY or CHECKSEQUENCEVERIFY opcode must be followed by OP_DROP. This is because OP_CHECKLOCKTIMEVERIFY (CLTV) does not consume its input — it leaves the lock time value on the stack. The OP_DROP that follows cleans up this residual value.
OP_CHECKLOCKTIMEVERIFY (CLTV) pattern:
<expiry_time> OP_CHECKLOCKTIMEVERIFY OP_DROP
<pubkey> OP_CHECKSIG
Without the OP_DROP, the lock time integer would remain on the stack and could cause script evaluation to return an unexpected result.
Usage in P2SH Redeem Scripts
In P2SH contexts, OP_DROP is frequently used to clean up stack items pushed by the scriptsig that were used for routing or versioning purposes:
OP_IF
<branch_data> OP_DROP
<pubkey1> OP_CHECKSIG
OP_ELSE
<pubkey2> OP_CHECKSIG
OP_ENDIF
Here, <branch_data> is pushed to help select a branch but is dropped once the correct branch is entered.
Edge Cases and Failure Conditions
-
If the stack is empty when
OP_DROPis called, the script will fail. -
OP_DROPdoes not interact with the alternate stack. It only affects the main stack. -
In Tapscript (BIP 341/342),
OP_DROPretains the same behavior as in legacy script.
Security Considerations
While OP_DROP itself is benign, improper use can introduce logic bugs. For instance, dropping a value that was supposed to be verified results in unintentional permission grants. Developers must audit every OP_DROP call to ensure they are not discarding values that carry security-critical information.
Summary
OP_DROP is a stack hygiene opcode. It is used to discard values that have already served their purpose, keep the stack clean for subsequent operations, and satisfy the interpreter's requirement that the stack contain exactly one TRUE value upon script completion.
TeachMeBitcoin is an ad-free, open-source educational repository curated by a passionate team of Bitcoin researchers and educators for public benefit. If you found our articles helpful, please consider supporting our hosting and ongoing content updates with a clean donation: