<p>If <code>gettxout</code> returns something, the output is unspent. If it returns nothing, the output either never existed or is spent.</p>

<p>You do not need <code>-txindex</code> for this.</p>

CC BY-SA 3.0
2016-09-10T08:06:49.193Z
I see.  So, is there a way to tell which tx spent it? Also, is the `Unconfirmed` parameter relevant here in some way?
shx2
CC BY-SA 3.0
2016-09-10T08:24:56.070Z
No, there is nothing in Bitcoin Core that keeps track of which tx spent something. Once a UTXO is spent, it is simply removed from the database. If Unconfirmed is set to true, it will treat transactions in the mempool as if they were in a block (so it will list their outputs, and remove their inputs).
Pieter Wuille
CC BY-SA 3.0
2017-10-29T16:46:41.357Z
The dev reference states: `The gettxout RPC returns details about a transaction output. Only unspent transaction outputs (UTXOs) are guaranteed to be available.` This means spent txos may be available, too.  
By playing with RPC, it seems like in practice if it doesn't return anything it's indeed unspent, but is that guaranteed? Is the dev reference wrong?
https://bitcoin.org/en/developer-reference#gettxout
nopara73
CC BY-SA 3.0
2017-10-29T18:27:12.977Z
@nopara73 Yes, that's incorrect. It gives information about unspent outputs, and won't return anything for spent outputs.
Pieter Wuille
CC BY-SA 3.0
2017-10-30T21:41:18.143Z
@PieterWuille Thanks, made a PR for the fix in the docs: https://github.com/bitcoin-dot-org/bitcoin.org/pull/1891
nopara73
CC BY-SA 3.0
2016-09-10T07:48:30.703Z
2016-09-10T07:48:30.703Z
Pieter Wuille
<p>As @pieter-wuille already answered the first half of your question:  </p>

<blockquote>
  <p>If <code>gettxout</code> returns something, the output is unspent. If it returns nothing, the output either never existed or is spent.  </p>
</blockquote>

<p>For that I'd add you can set the unconfirmed flag <code>true</code>, so it factors in unconfirmed transactions, too.  </p>

<p>To answer the second part of your question, here's a sub-optimal way to find the transaction which spent your txo:<br>
This assumes <code>txindex=1</code> and <code>prune=0</code>.</p>

<p>To find out who spent the txout you can figure out the block it was confirmed with: <code>getrawtransaction {txid} 1</code>. From here on you can iterate through all the blocks to the last arrived one (<code>getblock</code>) and the mempool (<code>getrawmempool</code>), while checking every transaction:  </p>

<pre><code>foreach(var input in tx.inputs)
{
   if(input.prevout.txid == myTxo.txid &amp;&amp; input.prevout.index == myTxo.index)
   {
      // tx spent the txo you were looking for
   }
}
</code></pre>

CC BY-SA 3.0
2017-11-02T03:04:47.947Z
2017-11-02T03:04:47.947Z
nopara73
<p>A more optimal way to find the spending transaction:</p>
<ol>
<li>Find the output's descriptor and transaction timestamp: <code>bitcoin-cli getrawtransaction &lt;txid&gt; 2</code>.
It returns the output's descriptor in <code>vout[&lt;output index&gt;].scriptPubKey.desc</code> field and transaction timestamp in <code>time</code> field.</li>
<li>Create watch-only wallet <code>bitcoin-cli createwallet watchonly true</code></li>
<li>Add output's address to the created wallet with <code>bitcoin-cli -rpcwallet=watchonly importdescriptors '[{&quot;desc&quot;:&lt;descriptor&gt;,&quot;timestamp&quot;:&lt;tx-timestamp&gt;}]'</code></li>
<li>Invoke <code>bitcoin-cli -rpcwallet=watchonly listtransactions</code> and look at the records with <code>&quot;category&quot;: &quot;send&quot;</code> - one of those will be the spending transaction.</li>
</ol>

CC BY-SA 4.0
2024-07-12T09:47:13.993Z
2024-07-12T09:47:13.993Z
nikicat
<p>Using an rpc client (e.g. <code>bitcoin-cli</code>), for a given transaction and index of an output of it, how can I tell if the output is currently an utxo, or if it has already been spent?</p>

<p>In the latter case, I'd also want to know the transaction which spent it.</p>

<p>I looked at the <code>gettxout</code> command, but it isn't clear to me how to extract the answer from the data it returns.</p>

<p>I'm running a full node with txindex=1.</p>

<bitcoind><json-rpc><utxo><bitcoin-cli>
How to check if an output has been spent?
shx2
# How to check if an output has been spent?

(Asked by: username_0 on 2016-09-10)

Using an rpc client (e.g. `bitcoin-cli`), for a given transaction and index of an output of it, how can I tell if the output is currently an utxo, or if it has already been spent?

In the latter case, I'd also want to know the transaction which spent it.

I looked at the `gettxout` command, but it isn't clear to me how to extract the answer from the data it returns.

I'm running a full node with txindex=1.

**Tags:** bitcoind, json-rpc, utxo, bitcoin-cli


---

## 3 answers

### Answer by username_1 on 2016-09-10. Score: 8

If `gettxout` returns something, the output is unspent. If it returns nothing, the output either never existed or is spent.

You do not need `-txindex` for this.

> **Comments:**
> **username_0**: I see.  So, is there a way to tell which tx spent it? Also, is the `Unconfirmed` parameter relevant here in some way?  
> **username_1**: No, there is nothing in Bitcoin Core that keeps track of which tx spent something. Once a UTXO is spent, it is simply removed from the database. If Unconfirmed is set to true, it will treat transactions in the mempool as if they were in a block (so it will list their outputs, and remove their inputs).  
> **username_2**: The dev reference states: `The gettxout RPC returns details about a transaction output. Only unspent transaction outputs (UTXOs) are guaranteed to be available.` This means spent txos may be available, too.   By playing with RPC, it seems like in practice if it doesn't return anything it's indeed unspent, but is that guaranteed? Is the dev reference wrong? https://bitcoin.org/en/developer-reference#gettxout  
> **username_1**: @username_2 Yes, that's incorrect. It gives information about unspent outputs, and won't return anything for spent outputs.  
> **username_2**: @PieterWuille Thanks, made a PR for the fix in the docs: https://github.com/bitcoin-dot-org/bitcoin.org/pull/1891  

---

### Answer by username_2 on 2017-11-02. Score: 1

As @pieter-wuille already answered the first half of your question:  

If gettxout returns something, the output is unspent. If it returns nothing, the output either never existed or is spent.  

For that I'd add you can set the unconfirmed flag true, so it factors in unconfirmed transactions, too.  
To answer the second part of your question, here's a sub-optimal way to find the transaction which spent your txo:
This assumes txindex=1 and prune=0.
To find out who spent the txout you can figure out the block it was confirmed with: getrawtransaction {txid} 1. From here on you can iterate through all the blocks to the last arrived one (getblock) and the mempool (getrawmempool), while checking every transaction:  
foreach(var input in tx.inputs)
{
   if(input.prevout.txid == myTxo.txid && input.prevout.index == myTxo.index)
   {
      // tx spent the txo you were looking for
   }
}


---

### Answer by username_3 on 2024-07-12. Score: 0

A more optimal way to find the spending transaction:

1. Find the output's descriptor and transaction timestamp: `bitcoin-cli getrawtransaction  2`.
   It returns the output's descriptor in `vout[].scriptPubKey.desc` field and transaction timestamp in `time` field.
2. Create watch-only wallet `bitcoin-cli createwallet watchonly true`
3. Add output's address to the created wallet with `bitcoin-cli -rpcwallet=watchonly importdescriptors '[{"desc":,"timestamp":}]'`
4. Invoke `bitcoin-cli -rpcwallet=watchonly listtransactions` and look at the records with `"category": "send"` - one of those will be the spending transaction.


---

